# Programming in C (Part 3)

## Appendix C.

Bitwise Operators

So, what are bitwise operators? As its name suggests, bitwise operators are used to perform calculations using binary digits (or bits). In the C source code, you’d have to use bitwise operators with decimal numbers.

The Bitwise AND Operator

The bitwise AND operator is & but beware – don’t confuse it with the logical AND operator.

But the usage of the bitwise AND and the logical AND operators is virtually identical – in each case, both have expressions on each side and the whole AND expression returns a value. However, all logical expressions return 1 or 0. Bitwise expressions are different as they can return any integer.

Take a 2 binary numbers like 1000 and 1101 (8 and 13 in decimal). Now, if we are using the bitwise AND operator, we’d compare the first binary digit (or bit, for short) of one with the first bit of the other. If both bits are 1, we’d conclude that the first bit of the answer is a 1. If both bits aren’t 1, we’d write a 0. We then move to the next bit and so on. Let’s look at a few examples: (ignore my incorrect use of the assignment operator for now!)

1000 & 1101 = 1000

00100 & 10111 = 00100

01111 & 11100 = 01100

/* Decimal equivalents

8 & 13 = 8

4 & 23 = 4

15 & 28 = 12 */

Note that used numbers are with the same number of bits – even if it means having to add zeros to the smaller numbers.

The Bitwise OR Operator

The bitwise OR operator is |.

This is similar to the bitwise AND operator, except that a bit of the result is a 1 if one or more of the corresponding bits is a 1. Examples:

1000 | 1101 = 1101

00100 | 10111 = 10111

01111 | 11100 = 11111

/* Decimal equivalents

8 | 13 = 13

4 | 23 = 23

15 | 28 = 31 */

The Bitwise XOR Operator

Here’s something new. There isn’t a logical XOR operator but the bitwise XOR operator is ^. This is sometimes known as the exclusive OR operator.

Basically, the resulting bit is 1 if either, but not both, of the corresponding bits is 1. If both are 1 or both are 0, the resulting bit is 0. Examples:

1000 ^ 1101 = 0101

00100 ^ 10111 = 10011

01111 ^ 11100 = 10011

/* Decimal equivalents

8 ^ 13 = 5

4 ^ 23 = 19

15 ^ 28 = 19 */

The Bitwise NOT Operator

For this, we write ~ followed by just one binary number. It changes all 1’s into 0’s and vice versa. This is also known as the bitwise complement operator.

If you think about it, this would be like, subtracting the binary number from the maximum possible number. But why?

Usually, for a normal integer, on 16-bit machines, the maximum is 111111111111111 (or 32767 in decimal, i.e. 215-1). But suppose we had ~ 000000000011111. The result would be 111111111100000 because we turn all 1’s into 0’s and vice versa. Now you have to figure out why ~ 000000000011111 equals 111111111111111 – 111111111100000.

If you are trying to work out the logical equivalent to XOR,  it’s rather complex! The 3rd block of expressions is the answer:

1 AND 1 = 1     1 OR 1 = 1     NOT 1 = 0

1 AND 0 = 0     1 OR 0 = 1     NOT 0 = 1

0 AND 1 = 0     0 OR 1 = 1

0 AND 0 = 0     0 OR 0 = 0

NOT (1 AND 1) = 0

NOT (1 AND 0) = 1

NOT (0 AND 1) = 1

NOT (0 AND 0) = 1

(1 OR 1) AND ( NOT (1 AND 1)) = 1 AND 0 = 0

(1 OR 0) AND ( NOT (1 AND 0)) = 1 AND 1 = 1

(0 OR 1) AND ( NOT (0 AND 1)) = 1 AND 1 = 1

(0 OR 0) AND ( NOT (0 AND 0)) = 0 AND 1 = 0

1 XOR 1 = 0

1 XOR 0 = 1

0 XOR 1 = 1

0 XOR 0 = 0

The Shift Operators

Shift operators are used when you wish to multiply or divide by “2 to the power of something”. They both take 2 operands: the number to be shifted, followed by the left << or right shift >> operator, then the number of bits to shift.

Take a binary number, for example, 00100 (4 in decimal). If we left shift it by 2 bits, we’d get 10000. If we we’re to shift it 2 bits to the right we’d end up with 00001.

Here’s another example. Consider 16 << 3. This would return 128 because it’s the equivalent of saying 16 * 23.

Similarly 16 >> 3 would return 2 because it’s the equivalent of saying 16 / 23.

Bitwise shift operations are sometimes performed as opposed to simple arithmetic in situations where effieciency is required – like in games programming for example. (According to my book on games programming!)

0
Liked it
No Responses to “Programming in C (Part 3)”
Post Comment