Skip to content

Verilog operators: a quick reference with examples

Verilog has several operators that look alike but behave differently on multi-bit values: & versus &&, unary & versus binary &. This page lists them with short examples.

Bitwise operators

Bitwise operators work on each bit position separately and return a value as wide as the operands.

Bitwise operators on a = 4'b1100 and b = 4'b1010.
OperatorMeaningExampleResult
~NOT~a4'b0011
&ANDa & b4'b1000
|ORa | b4'b1110
^XORa ^ b4'b0110
~^ or ^~XNORa ~^ b4'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.

Logical operators on the same a = 4'b1100 and b = 4'b1010.
OperatorMeaningExampleResult
!logical NOT!a1'b0
&&logical ANDa && b1'b1
||logical ORa || b1'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 0

Shift, relational and conditional

Other operators used in combinational logic.
OperatorMeaningExample
<< >>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 xa == b
=== !==case equality; compares x and z literallya === 4'bx10z
< <= > >=relationalcount < 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.

Verilog operator precedence, highest first.
LevelOperators
1unary + - ! ~ & ~& | ~| ^ ~^ ^~
2**
3* / %
4binary + -
5<< >> <<< >>>
6< <= > >=
7== != === !==
8binary &
9binary ^ ^~ ~^
10binary |
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.

See a | b & c evaluated: a | b & c

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.

Export a full-adder carry as Verilog: a & b | cin & (a ^ b)

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.