Operators 140917230056 Phpapp01
Operators 140917230056 Phpapp01
Most operators available are of the binary type. A binary operator takes on
two operands; the operator comes in between the two operands in the
assignment.
The arithmetic operators treat both the operands as numbers and return the
result as a number.
All net and reg operand values are treated as unsigned numbers.
The result of any arithmetic operation — with the “+” or “–” or with any of
the other arithmetic operators discussed later — will have an x value if any of
the operand bits has an x or a z value.
In both cases the result of the operation is a single bit of value 1 (true) or 0
(false).
if operands are not of the same size , the smaller operand is zero filled on
the MSB side , for example
If the operands are of unequal length, the shorter one is zero filled to
match the larger operand.
The operators in this category are only of two types – those to test the
equality and those to test inequality.
== (logical equality)
!= (logical inequality)
===(case equality)
!== (case inequality)
The result is (bit-wise) as wide as the wider operand.If the width of one of
the operands is less than that of the other, it is bit-extended. by filling zero
bits and the widths are matched.
If one of the operands has an x or z bit in it, the corresponding result bit is
x. Either operand can be a single variable or an expression involving
variables.
Operator Truth Table: bit wise operators
The truth tables for different types of bit-wise operators are given in Table
Note that an z input is treated as an x value.
Examples of bitwise operators:
If the operands are unequal in length, the smaller operand is zero filled on
the most significant side for example
‘b0110 ^ ‘b10000
Answer is ‘b10110
Shift Operators:
The << operator executes left shift operation, while the >> operator
executes the right shift operation.
In either case the operand specified on the left is shifted by the number of
bits specified on the right.
The vacated positions created as a result of the shifting are filled with
zeroes.
If the right operand is x or z, the result has an x value. If the right operand
is negative, the left operand remains unchanged.
Shift Operators contd..:
the shift operation shifts left operand by the right
operand number of times. It is a logical shift .The
vacated bits are filled with 0 , if the right operand
evaluates to an x or z, the result of the shift operation
is an x.
reg[0:7] Qreg;
Qreg= 4’b0111;
The two operands separate the three operators in the order shown.
If A is true, b is evaluated.
If A is false, c is evaluated.
cond_expr ? expr1:expr2
If cond_expr is true (that is ,has a value 1), expr 1 is selected, if cond_expr
is false (value 0) , expr2 is selected.
if cond_expr is an x or a z , the result is a bitwise operation on expr1 and
expr2 with the following logic: 0 with 0 gives 0, 1 with gives 1, rest are x.
here is an example
wire [0:2] student = marks >18 ? Grade_A : grade_c;
the expression marks > 18 is computed ; if true , Grade_A is assighned to
student , if marks <=18 , grade_C is assighed to student
Another example
always
#5 ctr = (ctr!=25) ? (ctr+1) : 5;
The expression in the procedural assignment says that if ctr is not equal to
25 increment ctr, else if ctr becomes 25, reset to 5.
Operator priority:
A clear understanding of the operator precedence makes room for a
compact design description. But it may lead to ambiguity and to inadvertent
errors.
Whenever one is not sure of the operator priorities, it is better to resort to
the use of parentheses and ensure clarity and accuracy of expressions.
Further, some synthesizers may not interpret the operator precedence
properly. These too call for the apt use of parentheses.
If division of (Q – R) is desired,
the expression has to be recast as P = (Q – R) / S;