Bitwise Operators – Free Guide

Bitwise OperatorsI have an exciting new eGuide available.

Most of this info is available in various places on my site, but I’ve collated it all together in one document, and updated it, so it’s easier for you to use.

What does it cover?

It’s a no-jargon guide to the four bitwise operators in C (AND, OR, XOR, NOT).

Not only does it walk you through exactly how each one works, with examples, it also provides explanations of how they would be used in programming and (as if that wasn’t enough), there’s a handy quick-start guide at the end which you can refer to so you know exactly when to use each operator and why.

It’s all written in a no-nonsense, easy to read format, that makes programming the joy it should be.

Hurrah to that, I say.

 

 

 

Bitwise RGBA Values

Let’s take a look at bit shifting in practice.

Say we have a variable called colour, that contains an RGBA value. If you have never had any experience with graphics, all you need to know is that the colours you see on your screen may be represented as a combination of four different variables – red, green, blue and alpha. The alpha value is usually a percentage to describe the opacity, while red, green and blue values are combined to describe the final colour.

RGBA values are usually stored in a single 32 bit integer, with 8 bits used for each component:

RRRRRRRR GGGGGGGG BBBBBBBB AAAAAAAA

All well and good, but imagine we need to know what the green value is independently of everything else. How can we extract this information? And moreover, how do we get a colour encoded into the variable in the first place?

Setting an RGBA value

Imagine we want to set our colour to a bright yellow, fully opaque. This uses the RGBA components:

R) 0xFF
G) 0xCC
B) 0x00
A) 0xFF

As binary this looks like:

11111111 11001100 00000000 11111111

OK, we could set the colour variable using a large number:

int colour = 4291559679;

but that isn’t a very intuitive (or re-usable) solution.

Instead we’ll add our components in one at a time using a mask, and shift them to the correct positions:

unsigned int colour =
0xFF | (0x00 << 8) | (0xCC << 16) | (0xFF << 24);

To fully break down what is happening here, let’s look at the binary behind the scenes:

The first step is a bitwise OR on 0xFF with 0x00 shifted left by 8 places:

0000 0000 1111 1111 // 0xff
0000 0000 0000 0000 // 0x00 << 8
___________________
0000 0000 1111 1111

The next step is a bitwise OR on the result with 0xCC shifted left 16 places:

0000 0000 0000 0000 1111 1111 // result
1100 1100 0000 0000 0000 0000 // 0xCC << 16
_____________________________
1100 1100 0000 0000 1111 1111

And finally a bitwise OR on the result with 0xFF shifted left 24 places:

0000 0000 1100 1100 0000 0000 1111 1111 // result
1111 1111 0000 0000 0000 0000 0000 0000 // 0xFF << 24
_______________________________________
1111 1111 1100 1100 0000 0000 1111 1111 // 0xFFCC00FF, or 4291559679

The final result is the number we want to assign to the colour integer.

Extracting an RGBA value

Now say we want to extract that green value from our colour integer. We can simply do the following:

int green = (colour & 0x00FF0000) >> 16;

What’s happening here?

First off, we’re masking our colour variable using bitwise AND to effectively “turn off” all the components that we aren’t interested in:

1111 1111 1100 1100 0000 0000 1111 1111
0000 0000 1111 1111 0000 0000 0000 0000
_______________________________________
0000 0000 1100 1100 0000 0000 0000 0000

Then we shift 16 places to the right to put our green component in the first byte:

0000 0000 0000 0000 0000 0000 1100 1100 // 0xCC

Simple! Now we know how to extract any component we choose by adjusting the mask and number of places shifted accordingly.

Bitwise operator summary

A quick guide to which operator to use when.

Bitwise AND

  • Use with a mask to check if bits are on or off
  • Turn off individual bits

Bitwise OR

  • Turn on individual bits

Bitwise XOR

  • Toggle bits on and off, like a switch

Bitwise NOT

  • Turn off individual bits with AND

Bitwise left and right shift

  • Extract bytes from longer variables
  • Insert bytes into longer variables
  • Multiplication and division by powers of 2 (but be cautious with signed integers, remainders and overflow)

This is not an exhaustive list, but a basic guide. Have fun with bitwise operators, and if you want more examples and ideas, have a look at this fantastic collection of code snippets from Sean Eron Anderson.

Bit Shifting

The left and right shift operators are the last two bitwise operators for us to look at.

In C (and C++), << and >> are used to shift bits around inside bytes. Not just in any random fashion – these operators move all the bits either to the left or to the right, exactly as their names imply.

Left bit shift

This is the easy one. No matter what your integer type (long, short, signed, unsigned), applying this operator always moves the bits to the left and pads with zeros from the right. The bits that move off to the left are discarded – they are gone forever and you can’t get them back.

For each place that the bits are shifted, it is the equivalent of multiplying by 2.

62 << 2 = 248    //or, multiply by 4

In binary:

0011 1110 //62
(00)1111 1000 // 248 (discarded) padded

The next example shows what happens if you use an integer that is not large enough to hold the result. 120 << 2 should give you 480, but since we are using a single byte, we lose bits from the left and the result is 224:

120 << 2 = 224

In binary:

0111 1000 //120
(01)1110 0000 // 224 (discarded) padded

Right bit shift

The right shift does the opposite of the left shift. If you are applying it to an unsigned integer this is always the case and it is always the equivalent of dividing by 2 for each place shifted (and it always rounds down towards zero).

62 >> 2 = 15    // or, divide by 4

In binary:

0011 1110 //62
0000 1111(10) // 15 padded (discarded)

Right shifting signed integers

If you apply a right shift to a signed integer, then the result may vary. On some machines, under some compilers, it will pad with sign bits, i.e. 1s for a negative number and 0s for a positive number, since the leftmost bit is the sign bit for signed integers. This is also termed the ‘arithmetic shift’. On other machines, with other compilers, it will always pad with zeros. This is termed the ‘logical shift’.

GCC, which is the compiler I use, implements the arithmetic shift. You can easily write a few lines of code to test out what your own compiler does if you can’t find details in the documentation.

Note that right shifting a negative signed integer rounds towards negative infinity, not zero. -50 >> 2 will give -13, but -50 / 4 will give -12.

Two’s complement is used to represent signed integers, so the binary representation for the negative numbers below is probably not familiar. However, you can still see how the shift is applied here. (I’d like to cover two’s complement representation at some point in the future – it requires its own post to be explained effectively.)

1100 1110 // -50
1111 0011(10) // -13 padded (discarded)

What would I use bit shifting for?

We’ll take a look at some examples next week – right now I have a 3 week old baby that needs feeding!

Bitwise NOT

Well, we’re now onto the final of the four bitwise operators – bitwise NOT – probably the most simple of the four to understand.

Bitwise NOT is represented by the tilde symbol, ~, and only takes one operand.

Can I see an example?

~125 = 130

What is this actually doing?

Bitwise NOT essentially flips the bit setting for each bit, resulting in what is known as the “one’s complement” of the original number.

So the example above has a bit output as follows:

01111101    // 125
________
10000010    // 130

What would I use bitwise NOT for?

Turning off an individual bit

Actually, the NOT operator doesn’t itself turn off a bit, but in conjunction with the AND operator, you can use it with a mask to turn off a bit in a nice readable manner.

This is best illustrated with a full example:

char settings = 0;    // 00000000
char FLAG1 = 1;    // 00000001

Let’s assume that ‘settings’ is a byte we use to represent 8 boolean values. First of all, we’ll set the first bit of our settings variable to 1 using the OR operator and our FLAG1 mask:

settings = settings | FLAG1;

In binary this is:

00000000    // 0
00000001    // 1
________
00000001

If we want to turn that bit off again, instead of using AND and defining a new mask (11111110), we can use our FLAG1 mask and the NOT operator as follows:

settings = settings & ~FLAG1;

This reads quite intuitively: settings AND NOT FLAG1 turns off the first bit in settings. In binary:

00000001    // 1
11111110    // 254
________
00000000    // 0 – back to our original number

Note that applying the NOT operator to the flag generates the same binary sequence we would need to define as a new mask if we wanted to use AND alone (11111110). The NOT operator saves us a variable.

Can’t I just use XOR to turn this bit on and off?

Well spotted, yes you can!

settings = settings ^ FLAG1 // turns first bit on or off

The difference with XOR is a) you may not know if you are turning the bit on or off without checking the result (or input) as it’s just a switch, and b) XOR always has an effect, whether you intend it to or not.

With AND NOT you know you are explicitly turning a bit off, and if the bit is already off when you apply the mask, the result is unchanged.

Next week we’ll take a look at bit-shifting and summarise what we’ve covered with the four bitwise operators.