Menu
Coddy logo textTech

Reduction Operators

Teil des Abschnitts Grundlagen der Verilog-Journey von Coddy. Lektion 25 von 90.

Reduktionsoperatoren arbeiten mit all bits eines einzelnen Vektors und reduzieren sie auf ein Ergebnis aus single bit. Im Gegensatz zu bitweisen Operatoren, die zwei Zahlen Bit für Bit vergleichen, nehmen Reduktionsoperatoren eine Zahl und führen eine Operation über alle ihre Bits aus, um ein einzelnes Ergebnis zu erzeugen.

 

OperatorOperationErgebnis
&Reduktions-AND1, wenn all bits 1 sind
|Reduktions-OR1, wenn at least one bit 1 ist
^Reduktions-XOR1, wenn eine odd number von Bits 1 ist
~&Reduktions-NAND0, wenn all bits 1 sind
~|Reduktions-NOR0, wenn at least one bit 1 ist
~^Reduktions-XNOR1, wenn eine gerade Anzahl von Bits 1 ist

So funktionieren sie

Reduktions-UND (<strong>&</strong>):

&4'b1111 = 1   // all bits are 1
&4'b1011 = 0   // nicht alle Bits sind 1
&4'b0000 = 0   // all bits are 0

Reduktion ODER (<strong>|</strong>):

|4'b0000 = 0   // no bits are 1
|4'b0100 = 1   // mindestens ein Bit ist 1
|4'b1111 = 1   // all bits are 1

Reduktions-XOR (<strong>^</strong>):

^4'b1010 = 0   // two 1's (even) → 0
^4'b1000 = 1   // one 1 (odd) → 1
^4'b1111 = 0   // vier Einsen (gerade) → 0

Codebeispiel

module reduction_demo;
  reg [3:0] a, b, c;
  reg and_red, or_red, xor_red;
  
  initial begin
    a = 4'b1111;
    b = 4'b1010;
    c = 4'b1000;
    
    and_red = &a;      // 1111 → 1
    or_red  = |b;      // 1010 → 1
    xor_red = ^c;      // 1000 → 1
    
    $display("&4'b1111 = %d", and_red);
    $display("|4'b1010 = %d", or_red);
    $display("^4'b1000 = %d", xor_red);
    $finish;
  end
endmodule

Ausgabe:

&4'b1111 = 1
|4'b1010 = 1
^4'b1000 = 1

Häufige Verwendungen

Prüfen, ob alle Bits 1 sind:

all_ones = &data;   // 1 wenn data == 8'b11111111

Prüfe, ob ein Bit 1 ist:

any_one = |data;    // 1 wenn data != 0

Parität prüfen (ungerade Anzahl von 1en):

odd_parity = ^data; // 1 bei ungerader Anzahl von 1en

Prüfe, ob alle Bits 0 sind:

all_zeros = ~|data; // 1 wenn data == 0
challenge icon

Aufgabe

Schreibe die korrekten Reduktionsausdrücke für jede Aufgabe.

Was zu tun ist:

  1. Prüfe, ob alle Bits von a 1 sind, und speichere das Ergebnis in all_ones
  2. Prüfe, ob irgendein Bit von b 1 ist, und speichere das Ergebnis in any_one
  3. Prüfe, ob c eine ungerade Anzahl von 1's enthält, und speichere das Ergebnis in odd_parity

Probier es selbst

module reduction_challenge;
  reg [3:0] a, b, c;
  reg all_ones, any_one, odd_parity;
  
  initial begin
    a = 4'b1111;
    b = 4'b0100;
    c = 4'b1011;
    
    all_ones  = ______;   // all bits 1?
    any_one   = ______;   // any bit 1?
    odd_parity = ______;   // odd number of 1's?
    
    $display("&4'b1111 = %d", all_ones);
    $display("|4'b0100 = %d", any_one);
    $display("^4'b1011 = %d", odd_parity);
    $finish;
  end
endmodule
quiz iconTeste dich selbst

Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.

Alle Lektionen in Grundlagen

Übe selbstständig: Online-Verilog-Compiler