C Bitwise Operators – Learn Bitwise Operations in C
In C, bitwise operators allow you to perform operations directly on the binary representation of numbers. Bitwise operations are used for tasks like setting, clearing, or toggling specific bits in data. In this tutorial, we will explore the different types of bitwise operators and how they work in C.
๐น What are Bitwise Operators?
Bitwise operators are used to manipulate data at the bit level. The most common bitwise operators are:
- AND (&): Performs bitwise AND operation.
- OR (|): Performs bitwise OR operation.
- XOR (^): Performs bitwise XOR operation.
- NOT (~): Performs bitwise NOT operation (complement).
- Left Shift (<<): Shifts the bits to the left.
- Right Shift (>>): Shifts the bits to the right.
๐ Example 1: Bitwise AND (&)
The AND operator compares corresponding bits of two operands. The result is 1 if both bits are 1; otherwise, the result is 0.
#include <stdio.h> int main() { int a = 5, b = 3; // Perform bitwise AND int result = a & b; printf("a = %d, b = %d\n", a, b); printf("a & b = %d\n", result); // Output: 1 (0101 & 0011 = 0001) return 0; }
๐ Example 2: Bitwise OR (|)
The OR operator compares corresponding bits of two operands. The result is 1 if at least one of the bits is 1.
#include <stdio.h> int main() { int a = 5, b = 3; // Perform bitwise OR int result = a | b; printf("a = %d, b = %d\n", a, b); printf("a | b = %d\n", result); // Output: 7 (0101 | 0011 = 0111) return 0; }
๐ Example 3: Bitwise XOR (^)
The XOR operator compares corresponding bits of two operands. The result is 1 if the bits are different, and 0 if they are the same.
#include <stdio.h> int main() { int a = 5, b = 3; // Perform bitwise XOR int result = a ^ b; printf("a = %d, b = %d\n", a, b); printf("a ^ b = %d\n", result); // Output: 6 (0101 ^ 0011 = 0110) return 0; }
๐ Example 4: Bitwise NOT (~)
The NOT operator inverts each bit of its operand, changing 1s to 0s and 0s to 1s.
#include <stdio.h> int main() { int a = 5; // Perform bitwise NOT int result = ~a; printf("a = %d\n", a); printf("~a = %d\n", result); // Output: -6 (5 in binary is 0101, its complement is 1010) return 0; }
๐ Example 5: Left Shift (<<)
The left shift operator shifts the bits of a number to the left by a specified number of positions, adding 0s on the right.
#include <stdio.h> int main() { int a = 5; // Perform left shift int result = a << 1; printf("a = %d\n", a); printf("a << 1 = %d\n", result); // Output: 10 (5 << 1 = 10) return 0; }
๐ Example 6: Right Shift (>>)
The right shift operator shifts the bits of a number to the right by a specified number of positions, effectively dividing the number by powers of 2.
#include <stdio.h> int main() { int a = 5; // Perform right shift int result = a >> 1; printf("a = %d\n", a); printf("a >> 1 = %d\n", result); // Output: 2 (5 >> 1 = 2) return 0; }
๐ฏ Key Takeaways
- Bitwise operators work at the binary level and are used for manipulating individual bits.
- The most common bitwise operators are AND, OR, XOR, NOT, left shift, and right shift.
- Bitwise operations are essential in low-level programming and optimization tasks.
๐ Practice Time!
Try experimenting with different numbers and bitwise operators. You can also explore how to use bitwise operations for tasks like setting and clearing specific bits, and implementing efficient algorithms!