Welcome to this comprehensive guide on C Bitwise Operators! In this lesson, we'll delve deep into the world of bit manipulation, a powerful tool in C programming that allows you to perform low-level operations and write efficient code. Let's get started! 📝
Before we dive into the specific operators, let's first understand what bitwise operators are and why they're essential.
Bitwise operators work on the individual bits (0s and 1s) of integers. They allow you to perform operations like setting, resetting, or toggling bits, comparing numbers on a binary level, and more. Bitwise operators can help you write compact, efficient code and better understand the underlying binary representation of numbers.
C provides several bitwise operators, which are:
&)|)^)~)<<)>>)&)The & operator performs a bit-by-bit comparison between two numbers. If both bits are 1, the result is 1; otherwise, the result is 0.
#include <stdio.h>
int main() {
int a = 10;
int b = 4;
int result = a & b;
printf("The result of a & b is: %d\n", result);
return 0;
}Output:
The result of a & b is: 2
|)The | operator performs a bit-by-bit comparison between two numbers. If at least one bit is 1, the result is 1; otherwise, the result is 0.
#include <stdio.h>
int main() {
int a = 10;
int b = 4;
int result = a | b;
printf("The result of a | b is: %d\n", result);
return 0;
}Output:
The result of a | b is: 14
^)The ^ operator performs a bit-by-bit comparison between two numbers. If the bits are different, the result is 1; otherwise, the result is 0.
#include <stdio.h>
int main() {
int a = 10;
int b = 4;
int result = a ^ b;
printf("The result of a ^ b is: %d\n", result);
return 0;
}Output:
The result of a ^ b is: 6
~)The ~ operator computes the bitwise complement of a number. This means it flips all the bits (0s become 1s, and 1s become 0s).
#include <stdio.h>
int main() {
int a = 10;
int result = ~a;
printf("The result of ~a is: %d\n", result);
return 0;
}Output:
The result of ~a is: -11
<<)The << operator shifts the bits of a number to the left by a specified number of positions. Zeros are added to the right side of the number.
#include <stdio.h>
int main() {
int a = 10;
int result = a << 2;
printf("The result of a << 2 is: %d\n", result);
return 0;
}Output:
The result of a << 2 is: 40
>>)The >> operator shifts the bits of a number to the right by a specified number of positions. Zeros are added to the left side of the number.
#include <stdio.h>
int main() {
int a = 10;
int result = a >> 2;
printf("The result of a >> 2 is: %d\n", result);
return 0;
}Output:
The result of a >> 2 is: 2
Now that we understand the bitwise operators let's look at a practical example where we use them to create a program that calculates the parity of a number. The parity of a number is its remainder when divided by 2. If the remainder is 0, the number is even; otherwise, it's odd.
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int parity = number & 1;
if (parity == 0) {
printf("The number %d is even.\n", number);
} else {
printf("The number %d is odd.\n", number);
}
return 0;
}What does the `&` operator do in C programming?