Using Bitwise Operators

Many programming languages for robot control support unique forms of operators that work with numbers only and let you manipulate the individual bits that make up those numbers. These are called bitwise operators. The following list shows the common characters or names used for bitwise operators. Note that some languages don’t distinguish between bitwise and relational (logical) operators.

Operator Symbol

Name

Function

Not or ~

Bitwise Not

Reverses the bits in a number; 1s become 0s and vice versa

And or &

Bitwise And

Performs And on each bit in a number

Or or |

Bitwise Or

Performs Or on each bit in a number

Xor or ^

Bitwise Xor

Performs Xor on each bit in a number

<<

Shift left

Shifts the values of the bits 1 or more bits to the left

>>

Shift right

Shifts the values of the bits 1 or more bits to the right

Let’s use some bitwise operators in an example. Suppose you assign 9 to variable This and 14 to variable That. If you bitwise And (& symbol) the two together, you get 8 as a result. Here are the binary equivalents of the values 9 and 14 (only 4 bits are shown, since 9 and 14 can be expressed with just 4 bits):

Decimal Number

Binary Equivalent

9

1001

14

1110

Using the And truth table given earlier, manually compute what happens when you bitwise And these two numbers together:



&

1001 = 9
1110 = 14
____
1000 = 8

Using the same numbers for a bitwise Or computation, we get:



|

1001 = 9
1110 = 14
____
1111 = 15

Here’s just one example of how this bitwise stuff is handy in robotics: bitwise And and bitwise Or are commonly used to control the output state of a robot’s computer or microcontroller I/O port. The port typically has eight output pins, each of which can be connected to something, like a motor. Suppose the following 4 bits control the activation of four motors. When “0,” the motor is off; when “1,” the motor is on.

0000

All motors off

0010

Second motor on

1100

Fourth and third motor on

. . . and so forth. Here the rightmost bit is motor 1, and the leftmost is motor 4.

Suppose your program has already turned on motors 1 and 4 at some earlier time, and you want them to stay on. You also want to turn motor 3 on (motor 2 stays off). All you have to do is get the current value of the motor port:

1001

Motors 1 and 4 on (decimal value equivalent is 9)

You then bitwise Or this result with

0100

Motor 3 on (decimal value equivalent is 4)

Here’s how the bitwise Or expression turns out:



|

1001 = 9
0100 = 4
____
1101 = 13

In this example, Or’ing 9 and 4 happens to be the same as adding the decimal values of the numbers together. This is not always the case. For example, Or’ ing 1 and 1 results in 1, not 2.
For your reference, the following table shows the binary equivalents of the first 16 binary digits, counting zero as the first digit and ending with 15. You can count higher by adding an extra 1 digit on the left. The extra digit increases the count by a power of two—31, 63, 127, 255, 511, 1023, and so on.

Decimal Number

In Binary

Decimal Number

In Binary

0

0000

8

1000

1

0001

9

1001

2

0010

10

1010

3

0011

11

1011

4

0100

12

1100

5

0101

13

1101

6

0110

14

1110

7

0111

15

1111