Chuyển đến nội dung

Bài 9 · Modules and Data Types

Verilog Operators: Bitwise, Logical, Reduction and More

A complete guide to Verilog operators: arithmetic, bitwise, logical, reduction, shift, relational, equality with x and z, conditional and precedence, with runnable C equivalents.

Verilog has a rich operator set that looks like C but behaves differently in important places. The big differences: operators work on vectors bit by bit, there are reduction operators that collapse a vector to one bit, and the equality operators have versions that deal with x and z. This lesson walks through all groups and shows what each does in hardware.

Arithmetic

+ - * / % **

Addition and subtraction synthesize to adders; * synthesizes to a multiplier (a large block, or an FPGA DSP slice); / and % are expensive and usually only synthesizable when the divisor is a constant power of two. ** is exponentiation, mostly used on constants.

The width of an arithmetic expression is the widest operand, including the target of an assignment. So wire [8:0] s = a + b; with 8-bit a, b keeps the carry; wire [7:0] s = a + b; drops it.

Bitwise

Operator Meaning
~a NOT each bit
a & b AND bit by bit
a | b OR bit by bit
a ^ b XOR
a ~^ b XNOR (also ^~)

These produce a vector as wide as the operands: 4'b1100 & 4'b1010 is 4'b1000.

Logical

! && || treat a whole vector as one truth value: zero is false, any non-zero is true, and the result is 1 bit. !a is NOT of the whole vector, not of each bit.

VERILOG
wire [3:0] a = 4'b0100;
~a   // 4'b1011  (bitwise)
!a   // 1'b0      (logical: a is non-zero, so true, so NOT is false)
a && 4'b0001   // 1'b1

Using & where you mean && (or vice versa) is the most common operator bug, and it often still simulates correctly on single bits, then fails on buses.

Reduction

A reduction operator is written as a unary operator before a vector and combines all its bits into one:

Operator Result
&d 1 if all bits are 1
|d 1 if any bit is 1
^d 1 if an odd number of bits are 1 (odd parity)
~&d, ~|d, ~^d Negated versions

^d is a parity generator; ~|d is "all bits are zero", useful for a zero flag. In hardware these are trees of gates.

Shift

Operator Name Fills with
a << n Logical left zeros
a >> n Logical right zeros
a <<< n Arithmetic left zeros (same as <<)
a >>> n Arithmetic right the sign bit, only if a is signed

Shifting by a constant is free wiring. Shifting by a variable amount builds a barrel shifter (a set of multiplexers). >>> only preserves the sign if the operand is declared signed; otherwise it behaves like >>.

Relational and equality

< <= > >= compare as unsigned unless both sides are signed.

Operator Meaning
==, != Logical equality; result is x if either side has x or z
===, !== Case equality: compares x and z literally; result is always 0 or 1

Use === in testbenches when you want to detect x outputs. Do not use it in synthesizable code: hardware has no x to compare.

VERILOG
4'b101x == 4'b101x    // x      (cannot tell)
4'b101x === 4'b101x   // 1

Conditional

cond ? a : b is a multiplexer. If cond is x, the result merges the two sides bit by bit (equal bits stay, differing bits become x).

VERILOG
assign y = sel ? in1 : in0;
assign max = (a > b) ? a : b;

Concatenation and replication

{a, b} and {n{a}} were covered in the vectors lesson.

Precedence (high to low)

  1. Unary ! ~ & | ^ + - (including reduction)
  2. **
  3. * / %
  4. + -
  5. << >> <<< >>>
  6. < <= > >=
  7. == != === !==
  8. &, then ^ ~^, then | (bitwise)
  9. &&, then ||
  10. ?:

Bitwise & ranks below ==, unlike what many expect. So a & b == c parses as a & (b == c). Always use parentheses in mixed expressions.

Signed traps

If any operand in an expression is unsigned, the entire expression is treated as unsigned, and signed operands are converted. A single unsigned literal can silently break a signed comparison:

VERILOG
reg signed [7:0] s = -1;
if (s < 0)      ...   // true: 0 is a signed integer literal
if (s < 8'd0)   ...   // false: 8'd0 is unsigned, so -1 becomes 255

Use $signed() and $unsigned() to convert deliberately.

Stand-in: Verilog operators in C

C's &, |, ^, ~ match the bitwise operators, but you need helpers for reductions. This is not Verilog: it shows the results of each group on 8-bit data, which you can match against the rules above.

C Stand-in: bitwise, logical and reduction operators (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The arithmetic right shift keeps the sign bit (0xFC); the logical shift fills with zeros (0x3C). The C >> on a signed value is arithmetic on virtually all compilers, which matches Verilog's >>> on a signed operand.

Stand-in: the precedence trap

C Stand-in: bitwise versus equality precedence (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C and Verilog share this precedence quirk, so the lesson transfers: parenthesize.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does ^data compute for an 8-bit vector?

    A unary ^ is a reduction operator, producing one bit that is odd parity.
  2. 2. What is !4'b0100?

    Logical NOT treats the vector as one truth value; non-zero is true, so the result is 0.
  3. 3. When is === different from ==?

    == gives x if any operand has x or z; === compares them literally and returns 0 or 1.
  4. 4. Which shift preserves the sign of a signed operand?

    is the arithmetic right shift; it fills with the sign bit when the operand is signed.

  5. 5. How is a & b == c parsed?

    Equality has higher precedence than bitwise AND, so parentheses are needed.

Summary

  • Bitwise operators work per bit; logical operators reduce a whole vector to true or false.
  • Reduction operators (&d, |d, ^d) collapse a vector to one bit: all ones, any one, parity.
  • == returns x when x or z is involved; === does not and is for testbenches only.
  • Shifts by constants are wiring; >>> needs a signed operand to keep the sign.
  • Mixing signed and unsigned makes the expression unsigned; parenthesize mixed expressions.

Next: Continuous Assignments with assign

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.