?
Solved

Bitwise And Operation

Posted on 1998-02-02
2
Medium Priority
?
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 80 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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