Contents

Once I was roaming the Visual C++ forum (again), I had to face the fact that bitwise operations, and binary in general, are rarely in beginner's common sense. After having pained my fingers to write a very long answer to that innocent person, it became obvious that I had to share this obfuscated knowledge with the community through this article.

This is obviously a beginners article, but if you want to get deeper knowledge about the C/C++ bitwise operators cover, you can read the very complete article, An introduction to bitwise operators by PJ Arends. You can also go into a very complex (but effective) analysis of bit hacking with the article, Bit Twiddling Hacks By Sean Eron Anderson.

I'm going to present in the most complete way that I can about what we can do with bitwise operators, flags and all that binary stuff.

One of the places where we most find the use for this is certainly when a library provides a set of enumerations and when functions use DWORD as a flags container. Let's take for that article the example of an enum which defines some styles:

Notice that for all of these values, only one bit is set at a time, all the others are equal to 0. You now can see a high level of interest appearing in this, which is that each bit is used as a flag for a functionality (here, each bit represents a style). We can now imagine a way to mix the flags together in one variable, to avoid using as many booleans as we need flags. Consider the following example :

0b 00000000 00000000 00000000 00100101
Flags of Style1, Style3 and Style6 are set

We face a problem now. C++ doesn't handle binary directly. We have to use bitwise operators instead. There are 3 atomic bitwise operators to know, presented by ascending order of priority : OR (|), AND (&) and NOT (~). Here are their behaviors:

We can see that the bitwise OR operator is pretty much like the addition (+) operator. However, you have to be very careful if you wished to use + instead or |. The reason is simple: adding 1 + 1 resolves into 0 (plus one carry over 1). You won't see any problem if all the constants are strictly different though, but I won't go deeper as it is a bad practice to use other than bitwise operators when processing binary operations.

Commonly, such mixes stick to the DWORD type. However, this is not a must as we can do this with any integer types (char, short, int, long...). A DWORD is an unsigned 32 bits integer (like those used in the binary representations of this article). Let's imagine the case when we have such a DWORD constructed in a function, and passed to another in parameter. How can the called function know which bits are set, and which are not? Easy... Follow me!

Say we want to know if the bit of STYLE8 is set in the DWORD passed in parameter. We must have a mask which we will call the AND parameter. In practice, the mask is the same as the constant we are about to test, so there's no additional code to create such a mask:

I didn't present the third operator NOT (~) yet. It is generally used when you have a set of bits, in which some are set and some aren't, and where you'd like to remove one of them. The sample of code below exposes how this can be done:

I didn't mention the XOR ( ^ ) operator yet. The reason why it comes last is for the only reason that this operator is not atomic; that means that we can reproduce its behavior with the other operators presented already:

Now, to perfect your sharpen skills on bits handling, there are few other operators you have to know to be unbeatable: the shift operators. Those can be recognized like this : << (left shifting, follow the arrows), >> (guess what, this is a right shifting).

What a shifting operator is moving the bits of n positions to the right or to the left. The "space" made by the movement is padded with zeros, and the bits pushed over the limit of the memory area are lost.

Shifting one bit right, then one bit left gives a different result from the beginning. That's because as we just saw, the "overflown" bits are lost, and the newly inserted ones are set to 0. But let's look closer. This is working anyway because we are working on integers. That is, dividing an odd number will not give a float (we don't care about the remaining 0.5).

Here, 127 / 2 should be 63.5, so it gives 63, due to the truncation.63 * 2 = 126, isn't it what we want ?!

Now we've seen all the useful operators, just know that all of them have their own assignment operator.

Thanks to Randor in the VC++ forum, here is a nice example of what is possible to do with such operators (Credit for the discovery of this little neat trick below goes to Sean Anderson at stanford university).

"How is it possible to reverse the bits of an integer, say from 11010001 to 10001011 ?"

Well, here we are then. If you came here to understand these binary operations, I hope that I helped you in your way. If you came to see what was going on here, then don't hesitate to point my mistakes if any, so that I can fix them.

Share

About the Author

Toxcct is an electronics guy who felt in love with programming at the age of 10 when he discovered C to play with Texas-Instruments calculators.

Few years later, he discovered "The C++ Language" from Bjarne Stroustrup ; a true transformation in his life.

Now, toxcct is experiencing the Web by developing Siebel CRM Applications for a living. He also respects very much the Web Standards (YES, a HTML/CSS code MUST validate !), and plays around with HTML/CSS/Javascript/Ajax/PHP and such.

_____

After four years of services as a Codeproject MVP, toxcct is now taking some distance as he doesn't like how things are going on the forums. he particularly doesn't accept how some totally ignorant people got the MVP Reward by only being arrogant and insulting while replying on the technical forums.