Solved

Bitwise And Operation

Posted on 1998-02-02
2
338 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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