Jump to content
  • Advertisement
Sign in to follow this  
musafir2007

Bitwise & vs logic == ?

This topic is 2634 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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..
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!