Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shift a std_logic_vector of n bit to right or left

Tags:

I have a vector signal tmp : std_logic_vector(15 downto 0)

I have to shift it to left or right of n bit. how can I realize this operation. I thought to concatenation operation but I didn't know how use it.

like image 624
Mazzy Avatar asked Jan 26 '12 12:01

Mazzy


People also ask

Which are bit shift instructions?

Shift instructions allow the bits of a register or memory byte to be shifted one bit place to the left or to the right. There are two types of shift instructions — logical and arithmetic. Logical shifts consider the contents of the register or memory byte to be just a bit pattern when the shift is made.

What is shift logical left?

A shift left logical of one position moves each bit to the left by one. The low-order bit (the right-most bit) is replaced by a zero bit and the high-order bit (the left-most bit) is discarded. Shifting by two positions is the same as performing a one-position shift two times.

What does bit shifting by 0 do?

1 in binary is 0001 , then bitshifting it by 0 won't do anything, which aligns with what you observed. So any number x << 0 is equivalent to x * 2^0 , which is x * 1 , which is just x .


1 Answers

Use the ieee.numeric_std library, and the appropriate vector type for the numbers you are working on (unsigned or signed).

Then the operators are sla/sra for arithmetic shifts (ie fill with sign bit on right shifts and lsb on left shifts) and sll/srl for logical shifts (ie fill with '0's).

You pass a parameter to the operator to define the number of bits to shift:

A <= B srl 2; -- logical shift right 2 bits 

Update:

I have no idea what I was writing above (thanks to Val for pointing that out!)

Of course the correct way to shift signed and unsigned types is with the shift_left and shift_right functions defined in ieee.numeric_std.

The shift and rotate operators sll, ror etc are for vectors of boolean, bit or std_ulogic, and can have interestingly unexpected behaviour in that the arithmetic shifts duplicate the end-bit even when shifting left.

And much more history can be found here:

http://jdebp.eu./FGA/bit-shifts-in-vhdl.html

However, the answer to the original question is still

sig <= tmp sll number_of_bits; 
like image 56
Martin Thompson Avatar answered Oct 07 '22 15:10

Martin Thompson