Introduction
In this post, I'll explain how bitwise operators work in C.
We'll start with binary code, bitwise operators, and at the end, I'll give you a beginner exercise.
Binary code
Computers work with binaries. It's the programming language they understand based on a 2-number system, 0s and 1s. They represent the off-and-on states of transistors inside a computer's microprocessor.
We can perform operations on the bits of the binary using bitwise operators.
Let's take a look at different numbers in binary values:
int num1 = 5; // Binary: 0101
int num2 = 10; // Binary: 1010
int num3 = 17; // Binary: 10001
int num4 = 255; // Binary: 11111111
int num5 = 1023; // Binary: 1111111111
Binary code to decimal numbers
Binary code to decimal numbers isn't hard. You take the power of two, starting at 0 to the right, and increment it by one as you move to the left.
To break it down, if we've the binary number 1110
:
Rightmost bit:
0 (the bit) x 2 ^ 0 = 0
Second rightmost bit:
1 (the bit) x 2 ^ 1 = 2
Third rightmost bit:
1 (the bit) x 2 ^ 2 = 2 x 2 = 4
Fourth rightmost bit:
1 (the bit) x 2 ^ 3 = 2 x 2 x 2 = 8
The entire equation: 0 + 2 + 4 + 8 = 14
.
So 1110
is equal to 14.
Bitwise Operators
This part can be confusing. But before we dive into each operator, one main thing you've to understand:
The position of the numbers in the binary matter. So when compared or calculated, it's as if you're stacking them on top of each other, going column by column.
There are 6 bitwise operators.
AND: &
Each bit of the output is 1 if the corresponding bits of both inputs are 1. Otherwise, it's 0.
int a = 12; // Binary: 1100
int b = 10; // Binary: 1010
int result = a & b; // Result in Binary: 1000 (Decimal: 8)
OR: |
Each bit of the output is 0 if the corresponding bits of both inputs are 0. Otherwise, it's 1.
int e = 12; // Binary: 1100
int f = 10; // Binary: 1010
int result = e | f; // Result in Binary: 1110 (Decimal: 14)
XOR: ^
Each bit of the output is 1 if the corresponding bits of the inputs are different. Otherwise, it's 0.
int j = 12; // Binary: 1100
int k = 10; // Binary: 1010
int result = j ^ k; // Result in Binary: 0110 (Decimal: 6)
COMPLEMENT: ~
int o = 12; // Binary: 1100
int result = ~o; // Result in Binary: 0011 (Decimal: 3)
Left Shift
Shifts the bits of the number to the left and fills the empty bits with zeros.
Below we're shifting each bit two times to the left.
int r = 3; // Binary: 0011
int result = r << 2; // Result: 1100 (Decimal: 12)
Right Shift
Shifts the bits of the number to the right.
Below we're shifting two times to the right.
int u = 16; // Binary: 10000
int result = u >> 2; // Result in Binary: 00100 (Decimal: 4)
Exercise
Is even or odd
Use bitwise operators to determine whether the function is odd or even.
Revising how to convert binaries to decimal numbers helps here. Think about the possible values and what scenario has to be true for it to be odd.
int is_even(int number) {
// TODO: Return 1 if number is even,
// 0 otherwise using bitwise operator.
// C doesn't have booleans, hence we use 1 to determine `true`
}