Verilog operators and expressions
Introduction¶
Verilog supports a rich set of operators and expressions to model arithmetic, logical, and bitwise operations. Understanding these is crucial for writing efficient and synthesizable RTL code.
Types of Operators¶
Verilog operators can be grouped into the following categories:
- Arithmetic Operators
 - Relational Operators
 - Logical Operators
 - Bitwise Operators
 - Reduction Operators
 - Shift Operators
 - Concatenation and Replication Operators
 - Conditional (Ternary) Operator
 
Arithmetic Operators¶
Used for addition, subtraction, multiplication, division, and modulus.
| Operator | Description | Example | 
|---|---|---|
+ |  Addition | y = a + b; |  
- |  Subtraction | y = a - b; |  
* |  Multiplication | y = a * b; |  
/ |  Division | y = a / b; |  
% |  Modulus (remainder) | y = a % b; |  
Example:
reg [7:0] a = 8'd10, b = 8'd3;
reg [7:0] sum, diff, prod, quot, rem;
always @(*) begin
  sum  = a + b;  // 13
  diff = a - b;  // 7
  prod = a * b;  // 30
  quot = a / b;  // 3
  rem  = a % b;  // 1
end
Relational Operators¶
Used to compare values, returning 1 (true) or 0 (false).
| Operator | Description | Example | 
|---|---|---|
== |  Equal | a == b |  
!= |  Not equal | a != b |  
> |  Greater than | a > b |  
< |  Less than | a < b |  
>= |  Greater or equal | a >= b |  
<= |  Less or equal | a <= b |  
Example:
reg flag;
always @(*) begin
  flag = (a >= b); // 1 if a >= b, else 0
end
Logical Operators¶
Used for Boolean logic; operate on 1-bit true/false values.
| Operator | Description | Example | 
|---|---|---|
&& |  Logical AND | if (a && b) |  
|| |  Logical OR | if (a || b) |  
! |  Logical NOT | flag = !a; |  
Example:
reg a, b, result;
always @(*) begin
  result = a && b; // 1 if both a and b are 1
end
Bitwise Operators¶
Operate on each bit individually; used extensively in RTL.
| Operator | Description | Example | 
|---|---|---|
& |  Bitwise AND | y = a & b; |  
| |  Bitwise OR | y = a | b; |  
^ |  Bitwise XOR | y = a ^ b; |  
~ |  Bitwise NOT | y = ~a; |  
^~ / ~^ |  Bitwise XNOR | y = a ^~ b; |  
Example:
reg [3:0] a = 4'b1010, b = 4'b1100, y;
always @(*) begin
  y = a & b;   // 1000
  y = a | b;   // 1110
  y = a ^ b;   // 0110
  y = ~a;      // 0101
end
Reduction Operators¶
Reduce multiple bits into a single-bit result.
| Operator | Description | Example | 
|---|---|---|
& |  AND all bits | &a |  
| |  OR all bits | |a |  
^ |  XOR all bits | ^a |  
~& |  NAND all bits | ~&a |  
~| |  NOR all bits | ~|a |  
~^ / ^~ |  XNOR all bits | ~^a |  
Example:
reg [3:0] a = 4'b1101;
reg result;
always @(*) begin
  result = &a; // 1 if all bits are 1, else 0
end
Shift Operators¶
Used to shift bits left or right.
| Operator | Description | Example | 
|---|---|---|
<< |  Logical left shift | y = a << 2; |  
>> |  Logical right shift | y = a >> 1; |  
<<< |  Arithmetic left shift | y = a <<< 1; |  
>>> |  Arithmetic right shift | y = a >>> 1; |  
Concatenation & Replication¶
Concatenation¶
Combine multiple signals:
reg [3:0] a, b;
reg [7:0] y;
always @(*) begin
  y = {a, b}; // a occupies [7:4], b occupies [3:0]
end
Replication¶
Repeat a value multiple times:
reg [1:0] a = 2'b10;
reg [5:0] y;
always @(*) begin
  y = {3{a}}; // repeats a three times: 101010
end
Conditional (Ternary) Operator¶
The conditional (ternary) operator in Verilog is a compact and efficient way to implement decision-making logic within expressions. It has the general form:
<condition> ? <expression_if_true> : <expression_if_false>;
Single Condition Example¶
assign y = sel ? a : b;
Multi-condition example¶
assign out = (mode == 2'b00) ? in0 :
              (mode == 2'b01) ? in1 :
              (mode == 2'b10) ? in2 : in3;
Operator Precedence¶
Precedence affects how expressions are evaluated. Commonly:
()- Unary: 
! ~ & | ^ - Multiplicative: 
* / % - Additive: 
+ - - Shift: 
<< >> <<< >>> - Relational: 
< <= > >= - Equality: 
== != === !== - Bitwise AND: 
& - Bitwise XOR/XNOR: 
^ ^~ ~^ - Bitwise OR: 
| - Logical AND: 
&& - Logical OR: 
|| - Conditional: 
?: - Assignment: 
= <= += -= *= /= %= ... 
Use parentheses "( )" to avoid ambiguity.
Summary¶
| Operator Type | Examples | Notes | 
|---|---|---|
| Arithmetic | + - * / % |  Works on vectors; width matters | 
| Relational | == != > < >= <= |  Returns 1-bit true/false | 
| Logical | && || ! |  Works on single-bit logic | 
| Bitwise | & | ^ ~ ^~ |  Operates on each bit individually | 
| Reduction | & | ^ ~& ~| ~^ |  Reduces vector to single bit | 
| Shift | << >> <<< >>> |  Logical/arithmetic shifts | 
| Concatenation | {a, b} |  Combines vectors | 
| Replication | {3{a}} |  Repeats vectors |