shift operators in verilog |
Verilog provides a left shift operator using << to shift the bits to the left. You can specify the number of bits that need to shift. See the following example
|
The statement
x = x<<2; |
shifts x by two places to the left. This example gives the following output
x before shift = 1100
x after shift = 0000
The LSB bits are padded with 0s after the shift.
The >> is used to shift the bits to the right. The MSB bits are padded with 0s after the shift.
As an example in
x = 4'b1100; x = x>>1; |
The value of x after the two statements is 0'b0110.
The left shift by one place can be interpreted as multiplication by 2. The left shift by 2 places means multiplication by 4.
An important consideration in left shift is that the bits are lost as we saw in the example below. But, if the variable on the left side of the assignment statement is wider than the variable on the right side of the assignment, the value may be preserved. See the following example.
|
The output in this case is
x before shift = 1100
y after shift = 110000
However, this does not apply in case of right shift operator, where the bits shifted out of the right side are
not required to be preseved. Hence the code
|
produces no surprise and output is
x before shift = 1100
y after shift = 000001
The least significant bits in case of right shift operator are always lost.
Synthesis tools may choose to apply this consideration of unequal widths of the operand on the left side of left shift operator.