Menu
Coddy logo textTech

Reduktionsoperatoren

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

Reduktionsoperatoren wirken auf alle Bits eines einzelnen Vektors und reduzieren diese auf ein Ergebnissignal von einem einzelnen 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.

 

OperatorOperationResult
&Reduktions-AND1, wenn alle bits 1 sind
|Reduktions-OR1, wenn mindestens ein bit 1 ist
^Reduktions-XOR1, wenn eine ungerade Anzahl an bits 1 ist
~&Reduktions-NAND0, wenn alle bits 1 sind
~|Reduktions-NOR0, wenn mindestens ein bit 1 ist
~^Reduktions-XNOR1, wenn eine gerade Anzahl an bits 1 ist

Wie sie funktionieren

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

Reduktions-OR (<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 1en (gerade) → 0

Code-Beispiel

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 Anwendungsfälle

Prüfen, ob alle Bits 1 sind:

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

Prüfen, ob irgendein 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üfen, 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. Überprüfe, ob alle bits von a 1 sind, und speichere das Ergebnis in all_ones
  2. Überprüfe, ob irgendein bit von b 1 ist, und speichere das Ergebnis in any_one
  3. Überprüfe, ob c eine ungerade number von 1en hat, 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