Link to home
Start Free TrialLog in
Avatar of larockd
larockd

asked on

Bitwise And Operation

I am confused on Bit Comparisons.  When doing a comparison of two bit values -> example

if ((nFlags & MK_LBUTTON)==MK_LBUTTON)
   cout .....

Assuming that MK_LBUTTON = 00000001 and nFlags was Unknown
If nFlags was equal to 01010101 , the if statement would be true?  or does nFlags have to 00000001?  Basically what I am asking is that when doing the comparision is it checking for only the one in the last bit or is it comparing the entire byte?  Can someone explain how this all works and what it might be useful for?

Thanks
DArrell
ASKER CERTIFIED SOLUTION
Avatar of faster
faster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of faster
faster

Sorry, I was so dumb when I gave the answer.

 if ((nFlags & MK_LBUTTON)==MK_LBUTTON)

As long as the last bit of nFlags is 1, it will be true.  It is the same as

 if ((nFlags & MK_LBUTTON)!=0) but the latter is more common.