## Bitwise XOR

Today we’ll look at the third out of four bitwise operators – bitwise XOR.

Bitwise XOR is represented by the symbol ^ and is an exclusive OR operation.

130 ^ 10 = 136

### What is this actually doing?

Bitwise XOR compares each bit setting and if either one is set, sets the corresponding output bit to 1. However, if both bits are set, then the corresponding output bit is set to zero.

So to clarify, both OR and XOR compare each bit setting and if either one is set, sets the corresponding output bit to 1. However, if both bits are set, OR sets the output to 1, while XOR sets the output to zero.

In short, XOR gives a result of 1 only if the two inputs are different.

So the example above has bit comparisons as follows:

10000010    // 130
00001010    // 10
________
10001000    // 136

### What would I use bitwise XOR for?

Let’s take a look at how we might use XOR.

### Toggling with with XOR

XOR is primarily used to toggle bits on and off. Let’s say we have a byte which represents 8 boolean values and we want to turn the first two off and on again. We can use the same mask to toggle the bits we are interested in if we use the XOR operator.

10000111    // 135
00000011    // 3
________
10000100    // 132

Applying the mask turns off the last two bits.

10000100    // 132
00000011    // 3
________
10000111    //135

Applying it again turns the bits back on.

All the other bits remain unchanged. XOR is just like applying a switch – very useful in low level programming.

Next week we’ll take a look at bitwise NOT. Have fun!

## Bitwise OR

Following on from last week’s look at the bitwise AND operator, this week I’m going to look at bitwise OR and provide an example of how you can use it.

Bitwise OR is represented by the | symbol.

130 | 10 = 138

### What is this actually doing?

Bitwise OR compares each bit setting and if either one is set, sets the corresponding output bit to 1. So in the example above, we can see the actual bit comparisons as follows:

10000010    //130
00001010    //10
________
10001010    //138

Note that it doesn’t matter if one or both bits are set in what we are comparing. As long as either one is set, the output bit is set to 1.

### What would I use bitwise OR for?

OK, an example follows – it’s much easier to understand the usefulness of these operators when you see them in action.

### Turning on an individual bit

Remember last week we used bitwise AND to turn off an individual bit? Well bitwise OR allows you to do the opposite. By using a mask with the bits set that we want to turn on, we can modify the individual bits in any byte.

Let’s say we have a byte, containing 8 bits, which are used as true/false values. Without changing the settings of any of the other bits, we want to set the first bit to be true. We apply the mask 1 (decimal) as follows:

11000100    // 196
00000001    // 1
________
11000101    // 197

The result is that the first bit (that is, the right-most bit) is now set to 1, and the other bit settings remain unchanged.

That’s all there is to it!

Next week we’ll look at bitwise XOR.

## Bitwise AND

I’m going to spend the next few weeks taking a look at the bitwise operators (AND, NOT, OR, XOR), and how they are used in C/C++.

How often you are faced with problems that use these operators depends on the field you are working in, and the coding preferences of the people that have gone before you. It’s entirely possible to code in C++ for years and never need to know the ins and outs of bits in a byte.

The bitwise operators enable you to work at a very fine-grained level, which is advantageous when you are dealing with limited space, or sending messages with limited capacity.

So let’s get going.

Bitwise AND is represented by the & symbol.

128 & 10 = 0

### What is this actually doing?

Bitwise AND compares each bit setting and if they match, sets the corresponding output bit to 1. So in the example above, we can see the actual bit comparisons as follows:

10000000    //128
00001010    //10
________
00000000    //0

### What would I use bitwise AND for?

Good question. The above example seems totally counter-intuitive, and quite clearly would make you wonder what use this would ever actually have. Below are two examples of using the & operator that should enable you to see its usefulness more clearly.

#### 1. Bit masking to check status

Let’s say I am using a byte to hold the true/false status of 8 items. I want to know if item 5 is true (set to 1). Since I can’t read bits individually, I can simply AND my byte with the appropriate mask and test the result. My mask in this case is decimal 16 (00010000).

01101011    //107
00010000    //16
________
00000000    //0

The result is 0 if bit 5 is not set.

01111011    //123
00010000    //16
________
00010000    //16

And the result is 16 (i.e. the value of the mask) if bit 5 is set.

#### 2. Turning off an individual bit

Now let’s say that I want to turn off bit 5. I can do this with the mask 239  (11101111):

01111011    //123
11101111    //239
________

01101011    //107

Can you see what’s happened here? The bitwise AND comparison checks each bit and sets the result to 1 if they match. Therefore, if I want to turn off bit 5, all I need to do is set my mask to contain zero at this position. That way, bit 5 will never be set to 1 in the result. Setting everything else to 1 in the mask ensures that all other bits remain unchanged.

Note that you can’t turn a bit on with the AND operator, since you must have two values of 1 to get the output of 1.

Next week we’ll take a look at bitwise OR.