12 Verilog Operators 18-01-2023
12 Verilog Operators 18-01-2023
Multiply * binary
Divide / binary
Add + binary
Subtract - binary
Modulus % binary
Arithmetic operator examples
a*b // multiply a and b a=3’b011 b=3’b010 d=4 e=3
a/b // divide a by b c=a * b // c= 3’b110
a+b // add a and b c= a / b // c= 1
c= a+b // c= 3’b101
a-b // subtract b from a c= a-b // c=3’b001
a%b // modulus of a by b c=d/e // c=1
• In arithmetic operations, if any operand bit has a value x, then the result of
the entire expression is x.
• The size of the result is determined by the size of the largest operand.
Logical operators
• Logical operator evaluates always to a one bit value either true(1) or false (0) or
x (unambiguous) . If any operand bit is either x or z it is equivalent to x
logical or || binary
a1 = 1’b0; // 0 is false;
a=10 b=00
a2 = 1’b1; // 1 is true
a && b // evaluates to 0 ( 1 && 0)
a1 && a2 // is 0 (false)
a=2’b1x b=2’b11
a1 || a2 // is 1 (true)
a || b // is unknown, evaluates to x.
!a2 // is 0 (false)
a = 5 b = 6 c = 2’1x
a >b // evaluates to 0
a <= b // evaluates to 1
b >= c // evaluates to x
Equality operators
• Logical equality operator (== !=) will return x if any of the operand
bit has x.
a !=b // evaluates to 1.
e===f // evaluates to 1.
f===g // evaluates to 0.
d == e // evaluates to x
Bitwise operators
Bitwise operations are performed on each bit of the operand
c = ~a; // c = 3’b000
c = a & b; // c = 3’b101
c = a | b; // c = 3’b111
c = a ^ b; // c = 3’b010
c = a | d; // c = 3’b1x1
Reduction operators
• Reduction operators are unary operators
Operation Symbol Operand
reduction and & unary
reduction nand ~& unary
reduction or | unary
reduction nor ~| unary
reduction xor ^ unary
reduction xnor ~^ or ~^ unary
Reduction operator Examples
x = 5’b01100
c = |x // c= 0|1|1|0|0 c=1
c = ^x // c=0^1^1^0^0 c=0
Shift operators
• Shift operator can be shift left or shift right
Example:
a = 4’b1011;
y = a >> 2; // y =4’b0010, 0’s filled in MSB
y = a << 2; // y = 4’b1100, 0’s filled in LSB
Concatenation operators
• Concatenation operator is used to append multiple operands.
• The operand must be sized.
Example:
a=3’b101; b=3’b111;
y = {a,b}; // y = 6’b101111
y = {a,b,3’b010}; // y =101111010
Replication operators
Replication operator is used to concatenate same number.
a=3’b101 b =2’b10
control out
1 I1
0 I2
Operator precedence