Systemverilog shift operator question

Hi

I want to Write a function to shift only odd bits for a 64-bit value by an odd shift number
for example, 64-bit values shift by 1,3,5,… but only odd bits to be shifted.

In reply to Svlearner:

That’s nice that you want to write this. If I give you the answer, then you would not have written it. I can suggest using a for-loop. It can be done in one statement if you do it in the correct direction.

In reply to dave_59:

Thanks for the push and hint Dave. I tried below code but i am confused on how to shift only odd bits. when we shift a vector all bits will be shifted right?


 bit[7:0] a = 8'b1100_0110 ;
  bit[7:0] b ;  
  initial begin
    for(bit[2:0] idx = 0 ; idx< 7 ; idx=idx+1'b1) begin
      if (idx[0] ) begin 
        b = a << idx ;
        $display("value of b %b",b);
        $display("value of idx %d",idx);
        $display("value of a a[%d] = %b ",idx,a[idx]);
      end
    end

In reply to Svlearner:

Can you give me an example of a 64-bit value that you want shifted by how much? which direction. what would be a valid result?

In reply to dave_59:

It can be any value shifted by odd value and left shift.

In reply to Svlearner:

Can you give me an example of a 64-bit value that you want shifted and what the result should be?

In reply to dave_59:

Hi Dave,

I got that as question from a friend from his interview so unfortunately i dont have examples… I was thinking… solution might not be using shift operator as shift operator will shift all the bits not just odd bits…

In reply to Svlearner:

That doesn’t mean you cannot create an example with the result you are looking for without writing any code. That way a solution someone writes can be vetted against the desired result.

1 Like

Here, i took a shot::

Bit [7:0] index;// – left shift value
Bit [63:0] num; // input number
bit [63:0] shift_num //outpu number

for(int i=0;i<63;i=i+1) begin
shift_num[i] = (i%2==1)? ((i-index>0)? num[i-index]: num[63-(index-i)]):num[i];
end

1 Like