A bitwise operator is an operation that acts on the individual bits of a binary number (or rather the bits of a pair of binary numbers). Bitwise operations have many uses in code and they are especially useful for manipulating registers in microcontrollers and sensors and for generating checksums for communication protocols. There are 4 different bitwise operators: OR, AND, XOR and NOT.
When performing bitwise operations, the corresponding bits of two numbers are compared. For example if two 8 bit numbers named X and Y are being operated on, the first bit of X and the first bit of Y will be manipulated, then the second bit of X and the second bit of Y, etc. The result of the operation for each bit does not have an effect on any other bit during the operation.
The result of a single bitwise operation is either a 1 or a 0. However since we are usually performing the operation on an 8-bit number the result ends up being a series of 8 1’s and 0’s. Here’s an overview of how each bitwise operator works, for each example assume we are operating on two 8-bit numbers named X and Y:
The OR operator will result in a 1 if either of the two operating bits are 1, or 0 if neither of the bits are 1. In other words, if X equals 1 ‘OR’ Y equals 1 then set the result to 1, otherwise set the result to 0. In most programming languages the symbol for the OR operator is ‘|’, this is also known as the pipe character and is found above the '\' on many keyboards.. Here’s an example of how the OR operator would work on a larger number:
X | Y = Result | X | 01001011 |
OR | ||
Y | 10001001 | |
Result | 11001011 |
The AND operator will result in a 1 if both of the two operating bits are 1, or a 0 if either of the bits is a 0. In other words, if X equals 1 ‘AND’ Y equals 1 then set the result to 1, otherwise set the result to 0. In most programming languages the symbol for the AND operator is ‘&’, this is also known as the ampersand character and is typically found above the '7.' Here’s an example of how the AND operator would work on a larger number:
X & Y = Result | X | 01001011 |
AND | ||
Y | 10001001 | |
Result | 00001001 |
The XOR operator will result in a 1 if one of the two operating bits are 1, but not if both of the operating bits are 1. In other words, if X is different than Y set the result to 1, otherwise set the result to 0. In most programming languages the symbol for the XOR operator is ‘^’, this is also known as the caret character and is typically found above the '6.' Here’s an example of how the XOR operator would work on a larger number:
X ^ Y = Result | X | 01001011 |
XOR | ||
Y | 10001001 | |
Result | 11000010 |
The NOT operator is a little different than the rest because it operates on a single number rather than two numbers. The NOT operator will result in a 1 if the operating bit is a 0, or a 0 if the operating bit is a 1. In other words, the result is the opposite of the operator. In most programming languages the symbol for the NOT operator is ‘~’, this is also known as the tilde character and is typically found to the left of the '1.' Here’s an example of how the NOT operator would work on a larger number:
NOT | |
X | 01001011 |
Result | 10110100 |
0b11001010 & 0b01011001 = ?