Menu
Coddy logo textTech

Reduktionsoperatoren

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

Reduktionsoperatoren arbeiten auf allen Bits eines einzelnen Vektors und reduzieren diese zu einem Einzelbit-Ergebnis. 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-UND1, wenn alle Bits 1 sind
|Reduktions-ODER1, 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

Wie sie funktionieren

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

&4'b1111 = 1   // Alle Bits sind 1
&4'b1011 = 0   // Nicht alle Bits sind 1
&4'b0000 = 0   // Alle Bits sind 0

Reduktions-OR (<strong>|</strong>):

|4'b0000 = 0   // kein Bit ist 1
|4'b0100 = 1   // mindestens ein Bit ist 1
|4'b1111 = 1   // alle Bits sind 1

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

^4'b1010 = 0   // zwei 1en (gerade) → 0
^4'b1000 = 1   // eine 1 (ungerade) → 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 Anwendungen

Prüfen, ob alle Bits 1 sind:

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

Prüfen, ob ein beliebiges Bit 1 ist:

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

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

odd_parity = ^data; // 1, wenn ungerade Anzahl von 1en

Prüfen, ob alle Bits 0 sind:

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

Aufgabe

Schreiben Sie die korrekten Reduktionsausdrücke für jede Aufgabe.

Was zu tun ist:

  1. Prüfen Sie, ob alle Bits von a 1 sind, und speichern Sie das Ergebnis in all_ones
  2. Prüfen Sie, ob irgendein Bit von b 1 ist, und speichern Sie das Ergebnis in any_one
  3. Prüfen Sie, ob c eine ungerade Anzahl von 1en hat, und speichern Sie das Ergebnis in odd_parity

Spickzettel

Reduktionsoperatoren wirken auf alle Bits eines einzelnen Vektors und reduzieren diese auf ein Ergebnis von einem einzelnen Bit.

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

Häufige Anwendungen:

all_ones   = &data;   // 1 if all bits are 1
any_one    = |data;   // 1 if any bit is 1 (data != 0)
odd_parity = ^data;   // 1 if odd number of 1's
all_zeros  = ~|data;  // 1 if data == 0

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