Solved

Bitwise And Operation

Posted on 1998-02-02
2
328 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:larockd
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 20 total points
ID: 1180080
Only when nFlag is 00000001, it will be true.

You have to understand what is the outcome for a bit AND.  For example, 01010101 & 00000001 = 00000001 and 01010101 & 00001111 = 00000101.

If what you want is the opposite, the code should be:
if ((nFlags & MK_LBUTTON) != 0)
0
 
LVL 7

Expert Comment

by:faster
ID: 1180081
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now