Menu
CoddyTech

Reduktionsoperatoren

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

Reduktionsoperatoren wirken auf alle Bits eines einzelnen Vektors und reduzieren sie auf ein einzelnes Bit als Ergebnis. Anders als bitweise Operatoren, die zwei Zahlen Bit für Bit vergleichen, nehmen Reduktionsoperatoren eine Zahl und führen eine Operation über alle ihre Bits hinweg aus, um ein einzelnes Ergebnis zu erzeugen.

 

OperatorOperationErgebnis
&Reduktions-AND1, wenn alle Bits 1 sind
|Reduktions-OR1, wenn mindestens ein Bit 1 ist
^Reduktions-XOR1, wenn eine ungerade Anzahl von Bits 1 ist
~&Reduktions-NAND0, wenn alle Bits 1 sind
~|Reduktions-NOR0, wenn mindestens ein Bit 1 ist
~^Reduktions-XNOR1, wenn eine gerade Anzahl von Bits 1 ist

So funktionieren sie

Reduktions-UND (&):

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

Reduktions-ODER (|):

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

Reduktions-XOR (^):

^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üfen, ob ein Bit 1 ist:

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

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

odd_parity = ^data; // 1, wenn die Anzahl der 1-Bits ungerade ist

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.

Aufgabe:

  1. Prüfe, ob alle Bits von a 1 sind, und speichere das Ergebnis in all_ones.
  2. Prüfe, ob ein beliebiges Bit von b 1 ist, und speichere das Ergebnis in any_one.
  3. Prüfe, ob c eine ungerade Anzahl von 1en 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