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 an unsigned
cast or assignment in to get the behaviour you want.
unsigned int y = x;
return ( y << offset ) | ( y >> (32 - offset));
Any ideas where it can come from or what I'm doing wrong ?
Sign-extension due to right shifting a signed value.
Am I using the correct int? should I use uint instead ? Or maybe char ?
Changing to unsigned int
in your function signature might be the easiest, yes.
Does it depends of the architecture of my computer (32 bits or 64 bits) ?
No, probably not.
I guess it depends of the length of the integer (that's why I'm using a 32 bits length number) ?
No, probably not. You could make your program not depend on the size of the integer, though:
return (y << offset) | (y >> ((sizeof y * CHAR_BIT) - offset));