Archived

This topic is now archived and is closed to further replies.

Shifting Values

This topic is 5172 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites