Jump to content
  • Advertisement
Sign in to follow this  
King of Men

Qustion about bitwise-AND-assign

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that
someBool &= someOtherBool;
is equivalent to
someBool = someBool & someOtherBool;
The question is, are they equivalent to
someBool = someBool && someOtherBool;
? It seems to me that they should be, since bools are implemented as a single bit - right? It would make my code look a lot prettier.

Share this post


Link to post
Share on other sites
Advertisement
Noooo! Bools are not a single bit. Usually they're the same size as a char. The actual size may be implementation-specific, I'm not sure about that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Red Ant
Noooo! Bools are not a single bit. Usually they're the same size as a char. The actual size may be implementation-specific, I'm not sure about that.
That doesn't stop them only using a single bit. Whether they do or not, I'm not sure. *Starts hunting around in the C++ Standard*

EDIT: I couldn't find any guarantees about the storage of a bool; only that it is an integral type that you can do bitwise operations on. Still, I'd imagine that most compilers you tested it on would have both results equal.

Share this post


Link to post
Share on other sites
bools are not a single bit. I don't think it's defined how big they are actually. In VC2005 sizeof(bool) == 1.

Nevertheless using & instead of && *should* work since bools are guaranteed to convert to 0 or 1. I probably still wouldn't recommend it though.

Share this post


Link to post
Share on other sites
Cut it out with bitwise ops on bools. Here's why:


bool a, b, c; a = b & c;

What does this do? If you answered "ANDs b and c, and stores the result in a", you get no cookie. What it does: It generates an integer representation of b which is 1 if b is true and 0 if b is false. Then it generates an integer representation of c which is 1 if c is true and 0 if c is false. Then it takes the bitwise AND of the two integers. If the result is 0, a is assigned to be true. If the result is nonzero, a is assigned to be false.

So overall this happens to have the effect of logical AND. But don't use it like that, because that's not what's really happening.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
That doesn't stop them only using a single bit. Whether they do or not, I'm not sure. *Starts hunting around in the C++ Standard*

The sizeof bool cannot be smaller than the sizeof char. This is because types are required to be uniquely addressable, and char is defined as the smallest addressable type.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
Quote:
Original post by Red Ant
Noooo! Bools are not a single bit. Usually they're the same size as a char. The actual size may be implementation-specific, I'm not sure about that.
That doesn't stop them only using a single bit. Whether they do or not, I'm not sure. *Starts hunting around in the C++ Standard*


Okay, let's look at the following example:


// The code below assumes that sizeof( bool ) == sizeof( unsigned char ).
unsigned char nSomeChar = 8;
unsigned char nSomeOtherChar = 2;
bool bMyFirstBoolean& = *reinterpret_cast < bool* > ( &nSomeChar );
bool bMySecondBoolean& = *reinterpret_cast < bool* > ( &nSomeOtherChar );

// Bitwise AND of 8 and 2 ==> 0 ... which evaluates to false.
bool bMyResultingBoolean = bMyFirstBoolean & bMySecondBoolean;

// Logical AND of 8 and 2 (same as logical AND of true and true) ... evaluates to true.
bool bTheTrueResult = bMyFirstBoolean && bMySecondBoolean;



Unless I've got a serious flaw in my thinking.




Share this post


Link to post
Share on other sites
This is something that you could easily test, and you should have before asking.
Now the question is, did you actually try and figure it out?

Now, on to the question.
Even though it's not something people would normally do, it does actually work.

If you use code and check EVERY possible way things could be, then it will work EVERY time. Now, in this case that proof is simple since bool only has two values, true and false.

So, do something like this:


void main()
{
bool bFalse = false;
bool bTrue = true;
bool bTest1, bTest2, bTest3;

bTest1 = true;
bTest1 &= bTrue;

bTest2 = true;
bTest2 = bTest2 & bTrue;

bTest3 = true;
bTest3 = bTest3 && bTrue;

if( bTest1 == bTest2 && bTest1 == bTest3)
std::cout << "True" <<endl;
else
std::cout << "False" << endl;

bTest1 = false;
bTest1 &= bTrue;

bTest2 = false;
bTest2 = bTest2 & bTrue;

bTest3 = false;
bTest3 = bTest3 && bTrue;

if( bTest1 == bTest2 && bTest1 == bTest3)
std::cout << "True" <<endl;
else
std::cout << "False" << endl;

bTest1 = true;
bTest1 &= bFalse;

bTest2 = true;
bTest2 = bTest2 & bFalse;

bTest3 = true;
bTest3 = bTest3 && bFalse;

if( bTest1 == bTest2 && bTest1 == bTest3)
std::cout << "True" <<endl;
else
std::cout << "False" << endl;

bTest1 = false;
bTest1 &= bFalse;

bTest2 = false;
bTest2 = bTest2 & bFalse;

bTest3 = false;
bTest3 = bTest3 && bFalse;

if( bTest1 == bTest2 && bTest1 == bTest3)
std::cout << "True" <<endl;
else
std::cout << "False" << endl;
}




And then everything comes out great, then you are good to go.
In this case the output I got was:

True
True
True
True

So, it works. There ya go. Next time you can try thiskind of method before asking. Otherwise, if you can't figure it out this way, come on back.

- Bnty_Hntr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!