Shifting Values

Started by
5 comments, last by LinaInverse2010 20 years, 6 months ago
If I''m only have a left shifter and I need to shift right, will this work: Reverse of Bits to shift (ie. 16-bits then bit 15 goes to bit 0, and 0 goes to 15) Shift them Reverse the output Will that be valid. I can''t see why not, but I just want to make sure. LinaInverse
Advertisement
Hmmm, I think it will work.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
seems like it should work... but you are probably better off writing your own function to shift bits to the right, it can''t be that hard...
Yeah I think just writing a plain old right-shift function would be easier than writing the "reverse bits" function (unless there is some easy way to reverse bits).
Actually I''m doing this in hardware, and another shifter would cost to much, so I want to reuse an old one that wouldn''t be being used at that moment.

LinaInverse
Ok. Well, there is one potential problem with your strategy- there are two kinds of right shifts, logical shifts and arithmetic shifts. For a logical shift, the empty bits on the left are always filled with 0''s (this is the more natural way of doing it). For an arithmetic shift on a signed number, the empty bits on the left are filled with the sign bit. So, if you right shift 11111101 by 1 (I''m using 8 bit numbers for simplicity), you will get 11111110 and not 01111110. In other words, right shifting -4 by 1 will give you -2 and not 126.

So you may not even want to support arithmetic shifts, but if you do then you''ll need a little extra code.
turn it upside down.
its now a left shifter

This topic is closed to new replies.

Advertisement