Bitwise operators
Bitwise operators work on each bit position separately and return a value as wide as the operands.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| ~ | NOT | ~a | 4'b0011 |
| & | AND | a & b | 4'b1000 |
| | | OR | a | b | 4'b1110 |
| ^ | XOR | a ^ b | 4'b0110 |
| ~^ or ^~ | XNOR | a ~^ b | 4'b1001 |
Logical operators
Logical operators treat a whole value as true when any bit is 1, and return a single bit. On one-bit signals they give the same result as the bitwise ones; on vectors they do not.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| ! | logical NOT | !a | 1'b0 |
| && | logical AND | a && b | 1'b1 |
| || | logical OR | a || b | 1'b1 |
A common bug: a & b where a = 4'b0100 and b = 4'b0010 is 4'b0000 (false), while a && b is 1 (true).
Reduction operators
Written as a unary operator before a vector, &, |, ^ and their negations ~&, ~| and ~^ combine all the bits of one value into a single bit.
wire [7:0] data;
assign all_ones = &data; // 1 only if every bit is 1
assign any_one = |data; // 1 if at least one bit is 1
assign parity = ^data; // 1 if an odd number of bits are 1
assign all_zeros = ~|data; // 1 only if every bit is 0Shift, relational and conditional
| Operator | Meaning | Example |
|---|---|---|
| << >> | logical shift left / right (fills with 0) | a << 1 |
| <<< >>> | arithmetic shift (>>> keeps the sign of a signed value) | s >>> 2 |
| == != | equality; x or z bits make the result x | a == b |
| === !== | case equality; compares x and z literally | a === 4'bx10z |
| < <= > >= | relational | count < 10 |
| ? : | conditional (a multiplexer) | sel ? d1 : d0 |
| { , } | concatenation | {a, b} |
Precedence
From highest to lowest, as listed in the Verilog standard (the operator precedence table in IEEE Std 1364-2005, the Verilog standard). Operators on the same row have equal precedence and, apart from ?:, group left to right. When in doubt, add parentheses: they cost nothing in hardware.
| Level | Operators |
|---|---|
| 1 | unary + - ! ~ & ~& | ~| ^ ~^ ^~ |
| 2 | ** |
| 3 | * / % |
| 4 | binary + - |
| 5 | << >> <<< >>> |
| 6 | < <= > >= |
| 7 | == != === !== |
| 8 | binary & |
| 9 | binary ^ ^~ ~^ |
| 10 | binary | |
| 11 | && |
| 12 | || |
| 13 | ?: (right to left) |
So a | b & c means a | (b & c), and a ^ b & c means a ^ (b & c). The simulator on this site uses the same order for the operators it supports.
Width and sign in expressions
Verilog sizes an expression from its operands and the place the result is assigned. Bitwise results are as wide as the widest operand; the shorter one is extended with zeros, or with its sign bit if both operands are signed. Relational, equality, logical and reduction operators always return one bit. Mixing a signed and an unsigned operand makes the whole expression unsigned, which is a frequent source of comparison bugs.
Four-state values
Real Verilog signals have four values: 0, 1, x (unknown) and z (high impedance). Bitwise AND with 0 gives 0 even if the other bit is x, but AND with 1 gives x. The simulator on this site uses only 0 and 1, which is enough for learning combinational logic and matches what synthesised hardware does once every input is driven.
From expression to module
The Verilog tab in the simulator turns an expression into a complete module with one input per variable and a continuous assign to the output. Input names that are Verilog keywords, such as wire, are written as escaped identifiers so the module still compiles, and the output is renamed if one of your inputs is already called y.
Sources and further reading
- IEEE Std 1364-2005, IEEE Standard for Verilog Hardware Description Language (operator precedence table)
- Wikipedia: Verilog
Every law, table and example on this page is checked by the automated tests of the simulator’s logic engine.