-
The ground is Rotating!
Here’s a Rotation function to recycle bits from the ‘bit bucket’,
Code:
unsigned char rol(unsigned char val) {
int highbit;
if(val & 0x80) // 0x80 is the high bit only
highbit = 1;
else
highbit = 0;
// Left shift (bottom bit becomes 0):
val <<= 1;
// Rotate the high bit onto the bottom:
val |= highbit;
return val;
}
Questions:
1. Why use 0x80(128) to check for the high bit?
2. Can you please explain the use of the Rotation?
3. Is there any other ways to do the Rotation?
Thanks.
-
We normally do not do other people's homework. Make a drawing with the bits and think about how the function works. Think about what the binary rerpesentation of 0x80 is.
-
Actually I understand the overall work of the function. It ANDs val and 0x80 to find if there’s a 1 in that position in val. But what I don’t understand is 0x80. Because, if we for example pass a value like 909, which has a 1 in the 8th position it works. The fallen bit after shifting is added to the end. But if we pass a value like 550, which 0 in 8th position it doesn’t Rotate the fallen bit?
-
You get an unsigned char to your function, so the range of values is (usually) 0-255 or 0x00-0xFF. Hence it doesn't make sense to check for 909 nor 550.
Yves