Something I find handy while programming is being able to use the bits in an integer-type variable as boolean flags. What I mean by that is, let’s say you have an INT that is 4 bytes in size. Each byte consists of 8 bits so this INT contains 32 bits in total (4 bytes * 8 bits in a byte). If we have a program that has 32 different boolean variables, wouldn’t it be more effective to store all of them in a single integer variable? This would especially be helpful when you want to keep your networking packets small. 32 booleans in 4 bytes instead of 32 bytes! Ultimately, it’s up to you to decide how to use it; I’m just here to show you how.

Now, I’m going to assume that you are already aware of how the binary number system works. If you don’t know about it, do a quick search on your favourite search engine and you will find many, many resources on the topic.

Setting up the Example

To keep the example small, we’re going to assume that the variable is simply 1 byte in size. This means that it has 8 bits. Each bit has a corresponding value which represents a power of two. Let’s see what this looks like:

The general formula for getting the decimal value of a bit is calculated as follows:

You can see from the above pattern that 2 to the power of the-position-of-the-bit-minus-one (or the “zero-based” position) will get us the decimal value that the bit represents. So the bit at position 4 has the value: 2 ^ (4 – 1) = 2 ^ 3 = 8

Checking the Bit Flags using Bitwise AND

Ok, we’ve got the bits and how to calculate them covered, but how do we use them as flags? I’m going to assume you understand how AND, OR and NOT work in binary and get to the good stuff.

To check the value of a bit flag, you can use the following formula:

This may look a little daunting, but it’s really not. Say you want to check position 4 from above. You would do:

This works because if you AND the value 8 (i.e. the bit in position 4 is high) with another value, you will get 0 unless the other value has the same bit high as well.

Let’s jump in and take a look at how this works in binary. We’ll assume our current variable’s decimal value is 157 and we want to know if the bit at position 6 is high.
Here it is in binary:

You can see here that it’s not, but how do we figure that out? Well, if we look at the decimal value at position 6 — 2 ^ ( 6 – 1) = 32 — and then AND those together:

You can see here, we get 0 after ANDing them. This means the bit at position 6 in the decimal value 157 is low (aka false, 0). Again, if you’re not sure why we get 0, there are plenty of resources online that describe how bitwise ANDing works. Just search for it. 🙂

Now, keeping in mind that the bit at position 6 is 32 in decimal, let’s change our saved flag value from 157 to 189 and see how it changes:

Now, you can see that since the bit in position 6 is high with the decimal value 189, when we AND them together we get the value (in this case, 32) back!

Setting the Bit Flag High using Bitwise OR

OK, so following the same principle for checking the bit flag, if we want to turn it high, we can simply bitwise OR it with the value of the flag.

Let’s look at the example of setting the bit in position 6 high with our decimal value 157. Again, keep in mind the decimal value for the bit at position 6 is 32.

Now, you’ll notice that when we OR them together we get the decimal value 189. Since the bit at position 6 is now high, our original value, 157, is increased by 32. You’ll also notice that if the bit had already been high, it would continue to be high because 1 OR 1 == 1.

Note: You may be thinking “Hey, so I can’t I just add/subtract 32 to set the bit?” This is only true if you know the bit is unset/set, respectively. For example, if you were trying to set the 6th bit high on the decimal value 189, and you added 32, you would get a totally different bit value because the 32 bit is already set. Long story short, use bitwise operators to be sure you’re doing it correctly.

Setting the Bit Flag Low using Bitwise NOT & AND

This is a little bit different (no pun intended 😀 ). To set a bit flag low, we have to NOT the value — which inverts its bits’ values — and then AND it.

Let’s have a look at this process by taking 189 and setting the 6th bit low.

Looking at this, you can see the bit at position 6 is now low but all the other bits are still high where they should be. You’ll also see that if the bit at position 6 was low already, it would stay low because 0 AND 0 == 0.

Yeah, that’s nice, but how do I use it?

That’s how it works. To use this I’ll give some examples in C#.

That’s all for now! Drop a comment if you have any questions or suggestions.

Post Scriptum

This post was started over 5 years ago on February 26, 2009 and has been left essentially untouched since then. Without further delay, here it is! Woo hoo!