I was trying to make some code in C++ about “bitwise rotation” and I would like to make this by the left shif. I didn’t know how to code this, but I found a little code in “Wikipedia” like this.
unsigned int rotl(unsigned int value, int shift) {
return (value << shift) | (value >> (sizeof(value) * CHAR_BIT - shift));
}
Then I tried to make it work, but this code don’t give the output that I expected. Ex. I have the number unsigned int 12
, in binary 1100, and when I want to do bitwise rotation by the left shif with the code above, the output is and unsigned int 24
,( 11000), and it had to give the output unsigned int 9
, because if I make the bitwise rotation(left shif), the first MSB bit have to be now the first bit, and all the others bits have to move one bit to left.
Can you help to understand what is the problem of that ?, or if I am doing something wrong.
Thank you.