Bitwise Manipulation: Logical and Shifting Operations4 min read
In the previous article, I’ve just introduced some concepts related to computer science and more specifically bitwise operation. We learned how what is bitwise operation is in general, and how to convert decimal, hexadecimal number to binary number, just in case you miss it, here are the helpful links before we go through:
- Introduction to Bitwise Operation
- How to convert the decimal, hexadecimal number to the binary number and vice versa
There are two types of bitwise operation and they are logical and shift operation, remember those ones? In this article, we will learn about all two types of those categories, the logical and the shift operation. Bitwise is an essential part of computer science, and also really useful when you doing some algorithms.
Logical bitwise operations are based on what it is defined in the truth table. In particular, truth tables can be used to show whether a propositional expression is true for all legitimate input values, that is, logically valid.
Bitwise AND &
Bitwise AND is the bitwise operator that requires two operands, denoted as &. The bits in the first operand are compared to corresponding bits in the second operand. When both bits are 1, then the resultant bit is 1. Otherwise the resultant is 0.
For example, 13 which equals 00001101 in binary & 7 which equals 000001111 results in 5:
binary decimal hexadecimal 00001101 13 0xd & 00000111 7 0x7 = 00000101 5 0x5
Bitwise OR |
The bitwise OR, denoted as | requires two operands, the bits in the first operand are compared corresponding bits in the second operands. When any of bits is 1, the resultant bit is 1.
For example, 12 which equals 00001100 and 6 which equals 00000110 results in 14:
binary decimal hexadecimal 00001100 12 0xc | 00000110 6 0x6 = 00001110 14 0xe
NOTE: We denote a hexadecimal number as 0x, while it’s 0b with binary.
Bitwise NOT ~
The bitwise NOT, also called bitwise complement, denoted as ~ it is used to flip every bit of an operand, which bit with the value 1 will turn to 0, and 0 turns to 1.
NOTE: A decimal number when is converted as a binary number sometimes just need 3 or 4 digits to indicate, but we also can add 0 to the left up to 8 digits without changing its value. It seems redundant in some cases, but in bitwise NOT, you should add some digits if necessary.
For example, 17 which equals 10001, do you think it will be 01110 which equals 14? But it’s not. We have to do it accurate:
~ 00010001 = 11101110 = 238 (decimal)
Bitwise Exclusive OR XOR
This operator requires two operands, which denoted as ^. It compares two corresponding each bit of two operands, return 1 if one bit is 0 and the other is 1, return 0 otherwise.
For example, 9 which equals 00001001 and 2 which equals 00000010 results 11.
binary decimal hexadecimal 00001001 9 0x9 | 00000010 2 0x2 = 00001011 11 0xb
Bitwise Shift Operators
Bitwise Left Shift <<
The bitwise left shift requires two operands, which denoted as <<. The bits on the first operands are shifted to the left by the number positions specified in the second operand. When bits are shifted to the left, high order bits are shifted out and 0s are shifted into low order bits.
For example we have the value of 7 which equals 0111 and we want to shift to the left 2 position:
00000111 << 2 = 00011100 = 28 (decimal)
The process is we remove the first two bits, then shift the bits to the left 2 positions, then add 0 to the right.
With x is the initial bits operand or in the decimal unit and y is the specified shifting position, we can quickly translate its value to decimal by:
x × 2y
For another example, we have the value of 23 we want to shift it to the left 4 positions:
23 × 24 = 23 × 16 = 368
Bitwise Right Shift >>
Denoted as >>, similar to left shift operator, which also requires two operands, instead of shifting to the left, it shifts bits to the right. The bits in the first operand are shifted to the right by the number of positions specified in the second operand. When bits are shifted to the right, low order bits are shifted out and 0s are shifted into high order bits.
For example, we want to shift the value of 69 (1000101) to the right 5 position:
69 01000101 0x45 >>5 = 2 00000010 0x2
We can also quickly calculate the value after shifting:
x ÷ 2y
x: the initial value
y: shifting positions (right operand)
For example we want to shift value of 32 to the right 4 positions:
32 ÷ 24 = 32 ÷ 16 = 2
But what should we do when the result is odd? It’s not the round number? In this case, the result is the largest round number which is smaller than the result.
For example we have 23 is a prime number and we want to shift it to the right 3 positions:
23 ÷ 23 = 23÷ 8 = 2.875, so the result is 2.