# [C++] Bit-Shifting

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

## 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 on other sites
It doesn't preserve it.
1010 shifted left one position: 0100

RCL and RCR (Rotate) do preserve the bit.

##### Share on other sites
Quote:
 Original post by KodeNerdI cannot remember if the bits are preserved in bit-shifting.

Bits that shift out are lost.

Quote:
 Original post by KodeNerdExample:1010 bit-shift leftis 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 on other sites
Thanks for the answers, that is where my errors were popping up.

##### 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 on other sites
Quote:
 Original post by xorIt doesn't preserve it.1010 shifted left one position: 0100RCL 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)

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 37
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631705
• Total Posts
3001827
×