# bool BitOne = (lNum & 0x1) 0

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

unsigned long CPWDeCodeDlg::PseudoShift(unsigned long lNum)
{
unsigned long lNewNum = lNum >> 1;  // bitwise shift right 1
bool BitOne = (lNum & 0x1) > 0;     // do logical & with bit 1 and the number
bool BitThree = (lNum & 0x4) > 0;   // do logical & with bit 3 and the number
lNewNum |= ((BitOne ^ BitThree) << 31);  // XOR BitOne with BitThree bitwise shift left 31
// Bitwise inclusive OR the 2 numbers
return lNewNum;                      // return value
}

This is someones code I was asked to look at from work but I'm not sure what >0 are for, do they stop the bits inverting if the number is below 0. thanks ABX

##### Share on other sites
No... they're not needed. Looks like the programmer just wasn't comfortable with int-to-bool conversions, or wanted to explicate what he was doing. If he was trying to write easlily readable code, though, then he should be chided for this whole function. The comments are downright stupid.

##### Share on other sites
Well, it's certainly not very nice code.
The comments are pretty much useless, since they just tell you what the code is doing and not why it's doing it. (They're also wrong, eg, "do logical & with bit 1 and the number" should really be "do bitwise & with bit 1 and the number")

As for the > 0, my best guess is that it was put in to prevent warnings about converting an integer to a bool; the expression "(lNum & 0x1)" results in an unsigned long, but it's being assigned to a bool, so to prevent warnings, an explicit check was put in. Since it's an unsigned integer, > 0 in this case is equivalent to != 0. (lNum & 0x1) will be 1 if the least significant bit is set, or 0 if the least significant bit is not set (at least the variable names here are correct).

Edit: Also, I have my own question about this code. Does the standard say anything about the representation of the 'bool' type, or what happens when you perform a bitwise xor with two bools? ie, if you do "((BitOne ^ BitThree) << 31)", can you be sure that the xor expression will always give you 1 or 0, and not, say, -1 (all bits set) or 0?

John B

##### Share on other sites
I'd agree with JohnBSmall. Coming from a strong typed language, where you can't just assign an int to a bool, you do the same way (if you wanted to determine if a bit was set - using that information to essentially do a shift like it is here is another story). The "> 0" is simply one way to test if any bits are set (since the & will leave only the specified bits set if they where set before). The better way to do it is to use the equivilent "!= 0" (which when compiled actually produces slightly faster code depending on compiler/processor), as that is more clear.

And yes, the comments are garbage. "bitwise shift right 1" says "I just learned C++ in nightschool".

##### Share on other sites
thanks for info guys I think he did his comments like that because it had to be sent to a parent company in germany and he was was into R&D electronics not programming. It was written VS3 in 1997.

##### Share on other sites
Quote:
 Original post by JohnBSmallEdit: Also, I have my own question about this code. Does the standard say anything about the representation of the 'bool' type, or what happens when you perform a bitwise xor with two bools? ie, if you do "((BitOne ^ BitThree) << 31)", can you be sure that the xor expression will always give you 1 or 0, and not, say, -1 (all bits set) or 0?

When converted to int, false is guarenteed to be 0 and true is guarenteed to be 1. Conversely, when converting to bool, zero becomes false and everything else becomes true.

CM

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633370
• Total Posts
3011542
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!