Operators and Operands


OPERATORS

Verilog provided many different operators types. Operators can be,

  • Arithmetic Operators
  • Relational Operators
  • Bit-wise Operators
  • Logical Operators
  • Reduction Operators
  • Shift Operators
  • Concatenation Operator
  • Replication Operator
  • Conditional Operator
  • Equality Operator

Arithmetic Operators

  • These perform arithmetic operations. The + and - can be used as either unary (-z) or binary (x-y) operators.
  • Binary: +, -, *, /, % (the modulus operator)
  • Unary: +, - (This is used to specify the sign)
  • Integer division truncates any fractional part
  • The result of a modulus operation takes the sign of the first operand
  • If any operand bit value is the unknown value x, then the entire result value is x
  • Register data types are used as unsigned values (Negative numbers are stored in two's complement form)

Relational Operators

Relational operators compare two operands and return a single bit 1or 0. These operators synthesize into comparators. Wire and reg variables are positive Thus (-3’b001) = = 3’b111 and (-3d001)>3d1 10, however for integers -1<>

Operator

Description

a <>

a less than b

a > b

a greater than b

a <= b

a less than or equal to b

a >= b

a greater than or equal to

  • The result is a scalar value (example a <>
  • 0 if the relation is false (a is bigger than b)
  • 1 if the relation is true ( a is smaller than b)
  • x if any of the operands has unknown x bits (if a or b contains X)

Note: If any operand is x or z, then the result of that test is treated as false (0)

Bit-wise Operators

Bitwise operators perform a bit wise operation on two operands. This take each bit in one operand and perform the operation with the corresponding bit in the other operand. If one operand is shorter than the other, it will be extended on the left side with zeroes to match the length of the longer operand.

 

Operator

Description

~

negation

&

and

|

inclusive or

^

exclusive or

^~ or ~^

exclusive nor (equivalence)

  • Computations include unknown bits, in the following way:
  • -> ~x = x
  • -> 0&x = 0
  • -> 1&x = x&x = x
  • -> 1|x = 1
  • -> 0|x = x|x = x
  • -> 0^x = 1^x = x^x = x
  • -> 0^~x = 1^~x = x^~x = x
  • When operands are of unequal bit length, the shorter operand is zero-filled in the most significant bit positions.

Logical Operators

Logical operators return a single bit 1 or 0. They are the same as bit-wise operators only for single bit operands. They can work on expressions, integers or groups of bits, and treat all values that are nonzero as “1”. Logical operators are typically used in conditional (if ... else) statements since they work with expressions.

Operator

Description

!

logic negation

&&

logical and

||

logical or

  • Expressions connected by && and || are evaluated from left to right
  • Evaluation stops as soon as the result is known
  • The result is a scalar value:
  • -> 0 if the relation is false
  • -> 1 if the relation is true
  • -> x if any of the operands has x (unknown) bits

Reduction Operators

Reduction operators operate on all the bits of an operand vector and return a single-bit value. These are the unary (one argument) form of the bit-wise operators.

Operator

Description

&

and

~&

nand

|

or

~|

nor

^

xor

^~ or ~^

xnor

  • Reduction operators are unary.
  • They perform a bit-wise operation on a single operand to produce a single bit result.
  • Reduction unary NAND and NOR operators operate as AND and OR respectively, but with their outputs negated.
  • -> Unknown bits are treated as described before

Shift Operators

Shift operators shift the first operand by the number of bits specified by the second operand. Vacated positions are filled with zeros for both left and right shifts (There is no sign extension).

Operator

Description

<<

left shift

>>

right shift

  • The left operand is shifted by the number of bit positions given by the right operand.
  • The vacated bit positions are filled with zeroes

Concatenation Operator

The concatenation operator combines two or more operands to form a larger vector.

  • Concatenations are expressed using the brace characters { and }, with commas separating the expressions within.
  • ->Example: + {a, b[3:0], c, 4'b1001} // if a and c are 8-bit numbers, the results has 24 bits
  • Unsized constant numbers are not allowed in concatenations

Replication Operator

Replication operator is used to replicate a group of bits n times. Say you have a 4 bit variable and you want to replicate it 4 times to get a 16 bit variable: then we can use the replication operator.

Operator

Description

{n{m}}

Replicate value m, n times

  • Repetition multipliers (must be constants) can be used:
  • -> {3{a}} // this is equivalent to {a, a, a}
  • Nested concatenations and replication operator are possible:
  • -> {b, {3{c, d}}} // this is equivalent to {b, c, d, c, d, c, d}

Conditional Operator

Conditional operator is like those in C/C++. They evaluate one of the two expressions based on a condition. It will synthesize to a multiplexer (MUX).

  • ->cond_expr ? true_expr : false_expr
  • The true_expr or the false_expr is evaluated and used as a result depending on what cond_expr evaluates to (true or false).

Equality Operators

There are two types of Equality operators. Case Equality and Logical Equality.

Operator

Description

a === b

a equal to b, including x and z (Case equality)

a !== b

a not equal to b, including x and z (Case inequality)

a == b

a equal to b, result may be unknown (logical equality)

a != b

a not equal to b, result may be unknown (logical equality)

  • Operands are compared bit by bit, with zero filling if the two operands do not have the same length
  • Result is 0 (false) or 1 (true)
  • For the == and != operators, the result is x, if either operand contains an x or a z
  • For the === and !== operators, bits with x and z are included in the comparison and must match for the result to be true

Note: The result is always 0 or 1


Operator Precedence

Operator

Symbols

Unary, Multiply, Divide, Modulus

!, ~, *, /, %

Add, Subtract, Shift

+, - , <<, >>

Relation, Equality

<,>,<=,>=,==,!=,===,!==

Reduction

&, !&,^,^~,|,~|

Logic

&&, ||

Conditional

? :