Solved

Bitwise And Operation

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
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…
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.

777 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