I wrote this little code in C:
int leftrotate (int x, int offset)
{
return ( x << offset ) | ( x >> (32 - offset));
}
I'm expecting to binary leftrotate an integer, meaning I'm expecting to move all the bits from the given offset to the left and boucling if the number is too big.
For example, when I input :
10100110100110100101000011111101 = 2795131133 = a69a50fd
I'm expecting as return :
01101001101001010000111111011010 = 1772425178 = 69a50fda
(notice that the hexadecimal a at the begining is now at the end because I choose offset 4 in this particular example)
But instead, I got:
11111111111111111111111111111010 = 4294967290 = fffffffa
- Any ideas where it can come from or what I'm doing wrong ?
- Am I using the correct
int
? should I useuint
instead ? Or maybechar
? - Does it depends of the architecture of my computer (32 bits or 64 bits) ?
- I guess it depends of the length of the integer (that's why I'm using a 32 bits length number) ?
Thanks !!!
It's implementation defined whether a right shift is an arithmetic or a logical shift. In your case, it appears to be an arithmetic shift, so you're getting sign extension in the
>>
half of your expression. You need to put anunsigned
cast or assignment in to get the behaviour you want.Sign-extension due to right shifting a signed value.
Changing to
unsigned int
in your function signature might be the easiest, yes.No, probably not.
No, probably not. You could make your program not depend on the size of the integer, though: