• Advertisement
Sign in to follow this  

[C++] Bit-Shifting

This topic is 3569 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

I cannot remember if the bits are preserved in bit-shifting. Example: 1010 bit-shift left is the outcome 0100 or 0101?

Share this post


Link to post
Share on other sites
Advertisement
It doesn't preserve it.
1010 shifted left one position: 0100

RCL and RCR (Rotate) do preserve the bit.

Share this post


Link to post
Share on other sites
Quote:
Original post by KodeNerd
I cannot remember if the bits are preserved in bit-shifting.

Bits that shift out are lost.

Quote:
Original post by KodeNerd
Example:

1010 bit-shift left
is the outcome 0100 or 0101?

It depends on the number of bits you are operating on. In a 32 bit integer, it works like this:
00000000000000000000000000001010 << 1 == 00000000000000000000000000010100

Share this post


Link to post
Share on other sites
Remember that result of a right shift of a signed negative quantity is implementation dependent. It may be a logical or arithmetic shift depending on the implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by xor
It doesn't preserve it.
1010 shifted left one position: 0100

RCL and RCR (Rotate) do preserve the bit.



You mean ROL and ROR

RCL and RCR perform rotates too, but through the carry bit (on a 32-bit register it is a 33-bit rotation, 32+carry)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement