|
-
February 14th, 2004, 02:28 PM
#1
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.
-
February 14th, 2004, 04:27 PM
#2
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.
-
February 14th, 2004, 09:56 PM
#3
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?
-
February 15th, 2004, 03:53 PM
#4
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
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|