Photo by Daniel Lanner on Unsplash
Mastering Bitwise Operators in C: A Beginner-Friendly Guide
Bitwise Operations with Simple Examples
I used to dread the bitwise operators topic and know I wasn't alone in that boat. Bitwise operators and operations are easy concepts once you grasp the basics. In this article, I will simplify bitwise operators in clear terms without any jargon.
Simply put, bitwise operators allow you to manipulate individual bits in a binary number. Take for example;
In binary, the number 1
is represented as 00000001
. Using bitwise operators, you can manipulate or set the second bit from right to 1
then the result will be 00000011
. In decimal, this is 3
.
It is safe to say that we are only manipulating 0 and 1 in bitwise operations.
I will introduce 6 Bitwise operators in this article;
Operator | Meaning of Operator |
& | Bitwise AND operator |
^ | Bitwise XOR or exclusive OR operator |
~ | Bitwise NOT operator |
<< | Left shift operator |
\>> | Right shift operator |
The truth tables and examples below summarize how these six bitwise operators work.
Bitwise AND (&) Operator.
Bitwise AND operator is simply a matching operator. It returns 1
if each bit is set to 1
it returns 0
if each of the two bits does not match.
Bitwise AND (&) |
0 & 0 = 0 |
0 & 1 = 0 |
1 & 0 = 0 |
1 & 1 = 1 |
#include <stdio.h>
int main() {
unsigned int num1 = 5; // Binary: 0000 0101
unsigned int num2 = 3; // Binary: 0000 0011
// Bitwise AND operation
unsigned int result_and = num1 & num2; // Binary: 0000 0001
printf("num1: %u\n", num1);
printf("num2: %u\n", num2);
printf("Bitwise AND Result: %u\n", result_and);
return 0;
}
Bitwise OR (|) Operator.
The bitwise OR operator is a merging operator. It compares two different bits and if at least one of the bits is 1
, the result after using the OR operator will be set to 1
.
Bitwise OR (|) |
0 | 0 = 0 |
0 | 1 = 1 |
1 | 0 = 1 |
1 | 1 = 1 |
#include <stdio.h>
int main() {
unsigned int num1 = 5; // Binary: 0000 0101
unsigned int num2 = 3; // Binary: 0000 0011
// Bitwise OR operation
unsigned int result_or = num1 | num2; // Binary: 0000 0111
printf("num1: %u\n", num1);
printf("num2: %u\n", num2);
printf("Bitwise OR Result: %u\n", result_or);
return 0;
}
Bitwise XOR Operator.
Bitwise XOR operator also known as the exclusive OR operator can be referred to as a toggle operator. It compares the corresponding bits of two numbers and if these corresponding bits are different, it sets the result to 1
but if they are the same, it sets the result to 0
.
Bitwise XOR (^) |
0 | 0 = 0 |
0 | 1 = 1 |
1 | 0 = 1 |
1 | 1 = 0 |
#include <stdio.h>
int main() {
unsigned int num1 = 5; // Binary: 0000 0101
unsigned int num2 = 3; // Binary: 0000 0011
// Bitwise XOR operation
unsigned int result_xor = num1 ^ num2; // Binary: 0000 0110
printf("num1: %u\n", num1);
printf("num2: %u\n", num2);
printf("Bitwise XOR Result: %u\n", result_xor);
return 0;
}
Bitwise NOT Operator.
The NOT operator is known as the negation operator. Popularly, it is referred to as the unary operator. unlike other operators that can handle multiple inputs, the NOT operator can only handle single digits. If the input is false (0
), the NOT operator returns true(1
), and if the input is negative(0
), the NOT operator returns positive (1
).
BITWISE NOT (~) |
~0 = 1 |
~1 = 0 |
#include <stdio.h>
int main() {
unsigned int num = 5; // Binary: 0000 0101
// Applying bitwise NOT to 'num'
unsigned int result = ~num; // Binary: 1111 1010
printf("Original Number: %u\n", num);
printf("Bitwise NOT Result: %u\n", result);
return 0;
}
Bitwise Left Shift Operator (<<).
The left shift operator shifts the bits of a value to the left by a specified number of positions.
Syntax of the Bitwise Left Shift Operator
value << n;
Where value
is the expression to which we apply the left shift operation and n
is the number of bits to be shifted
#include <stdio.h>
int main() {
unsigned int num = 5; // Binary: 0000 0101
// Left shift the number by 2 positions
unsigned int result = num << 2; // Binary: 0001 0100 (Decimal: 20)
printf("Original Number: %u\n", num);
printf("Left Shift Result: %u\n", result);
return 0;
}
We deleted the last two numbers from the left and added two zeros from the right.
Bitwise Right Shift Operator (<<).
The right shift operator shifts the bits of a value to the right by a specified number of positions.
Syntax of the Bitwise Right Shift Operator
value >> n;
Where value
is the expression to which we apply the right shift operation and n
is the number of bits to be shifted.
#include <stdio.h>
int main() {
unsigned int num = 20; // Binary: 0001 0100
// Right shift the number by 2 positions
unsigned int result = num >> 2; // Binary: 0000 0101 (Decimal: 5)
printf("Original Number: %u\n", num);
printf("Right Shift Result: %u\n", result);
return 0;
}