Public Group

Bitwise & vs logic == ?

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

Recommended Posts

I am really confused.. I am sure this is simple but I can't figure out the difference:

what's the difference between:
 if( (FilterMask() & TOUCH_FILTER) ) { //stuff... } and if( (FilterMask() == TOUCH_FILTER) ) { //stuff... } 

Thanks

Share on other sites
First, a quick primer on AND:

0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1

Now, assuming your input numbers are expressed binary:

1011101
0111011
======= AND
0011001

Make sense so far?

Now, compare:

1011101
0111011
======= EQUALS
0000000

The two numbers are obviously not equal, so == is going to be false (aka zero).

However, let's go back to AND for a second. What AND does is look at each bit in the binary number, and do a conversion based on the truth table I posted at the start - hence the name "bitwise and."

The result is a combination of bits; in this case, the bits we get are decidedly not all 0, so the result of & is true.

The magic sauce is this: TOUCH_FILTER is probably a power of two, which means that the binary expression has exactly 1 bit set to 1. When you & the FilterMask with this, you are basically seeing if the FilterMask shares that bit, i.e. the same bit is 1 in both input numbers.

By contrast, when you == with the FilterMask, you're checking if every single bit is the same, be it 0 or 1, in both numbers.

Make sense? :-)

Share on other sites
FilterMask probably returns a value with possibly several bits set. The first piece of code tells you if any of the bits set in TOUCH_FILTER are also set in FilterMask(). The second piece of code tells you if the bits set in TOUCH_FILTER are exactly the same as the bits set in FilterMask().

If you think of the bits as representing membership in a set, the first piece of code asks whether the two sets have non-empty intersection, and the second one whether they are equal.

In practice, TOUCH_FILTER most likely has a single bit set, so first you are asking if that bit is set, and then you are asking if exactly that bit is set and nothing else.

EDIT: I'm getting slow...

Share on other sites

I am really confused.. I am sure this is simple but I can't figure out the difference:

what's the difference between:
 if( (FilterMask() & TOUCH_FILTER) ) { //stuff... } and if( (FilterMask() == TOUCH_FILTER) ) { //stuff... } 

Thanks

As others have said, the end result is the same as long as FilterMask returns a value with only a single bit set, FilterMask however can most likely return a value with multiple bits set (Thats how bitmasks usually work). if you do for example:
TOUCH_FILTER = 1;
OTHER_FILTER = 2;
BLAHA_FILTER = 4;

int myFilterMask = TOUCH_FILTER | BLAHA_FILTER; (This sets myFilterMask to 5 or 101 binary)

then if (myFilterMask & TOUCH_FILTER) { stuff } will run stuff while if (myFilterMask == TOUCH_FILTER) { stuff } won't.

Share on other sites
Like the others have said FilterMask() probably returns a binary number where each bit represents 1 possible flag that is set. Each possible flag is a single bit at a different digit index (a power of 2)

More specifically it would be more explicit to write

 if( (FilterMask() & TOUCH_FILTER) == TOUCH_FILTER)) { // stuff.. } 

However, since anything nonzero is equivalent to true.. you will normally just see

 if( (FilterMask() & TOUCH_FILTER)) { // stuff.. } // or in a language such as Java or C# where integers are not automatically booleans... you could write if( (FilterMask() & TOUCH_FILTER) > 0) { // stuff.. } 

Share on other sites

Like the others have said FilterMask() probably returns a binary number where each bit represents 1 possible flag that is set. Each possible flag is a single bit at a different digit index (a power of 2)

More specifically it would be more explicit to write

 if( (FilterMask() & TOUCH_FILTER) == TOUCH_FILTER)) { // stuff.. } 

However, since anything nonzero is equivalent to true.. you will normally just see

 if( (FilterMask() & TOUCH_FILTER)) { // stuff.. } // or in a language such as Java or C# where integers are not automatically booleans... you could write if( (FilterMask() & TOUCH_FILTER) > 0) { // stuff.. } 

The two codes are not equivalent, except in a special case where the mask contains exactly one bit set. If the mask contains more than one bit set, the two codes will behave differently: the first will check if all bits in the mask are set, the second will check if any bit in the mask is set.

It is not clear whether the mask contains one or more bits set in this particular case, so it is very dangerous to imply that you can just exchange one with the other.

Share on other sites

[quote name='XZT123' timestamp='1317158017' post='4866575']
Like the others have said FilterMask() probably returns a binary number where each bit represents 1 possible flag that is set. Each possible flag is a single bit at a different digit index (a power of 2)

More specifically it would be more explicit to write

 if( (FilterMask() & TOUCH_FILTER) == TOUCH_FILTER)) { // stuff.. } 

However, since anything nonzero is equivalent to true.. you will normally just see

 if( (FilterMask() & TOUCH_FILTER)) { // stuff.. } // or in a language such as Java or C# where integers are not automatically booleans... you could write if( (FilterMask() & TOUCH_FILTER) > 0) { // stuff.. } 

The two codes are not equivalent, except in a special case where the mask contains exactly one bit set. If the mask contains more than one bit set, the two codes will behave differently: the first will check if all bits in the mask are set, the second will check if any bit in the mask is set.

It is not clear whether the mask contains one or more bits set in this particular case, so it is very dangerous to imply that you can just exchange one with the other.
[/quote]

well that's true if the mask can contain multiple bits. In that case, to be safe you should not do my 3rd example (commented java / c#) instead it should read..

 // or in a language such as Java or C# where integers are not automatically booleans... you could write (same as previous example) if( (FilterMask() & TOUCH_FILTER) == TOUCH_FILTER) { // stuff.. } 

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26