Bitwise operators in C are used to manipulate data at the bit level, allowing operations directly on individual bits of data.
These operators are particularly useful in applications that require low-level data manipulation, such as graphics, encryption, and systems programming.
1. Types of Bitwise Operators in C
Operator | Symbol | Description | Example |
---|---|---|---|
AND | & | Sets each bit to 1 if both bits are 1 | a & b |
OR | ` | ` | Sets each bit to 1 if at least one bit is 1 |
XOR | ^ | Sets each bit to 1 if only one bit is 1 | a ^ b |
NOT | ~ | Inverts all bits | ~a |
Left Shift | << | Shifts bits to the left | a << n |
Right Shift | >> | Shifts bits to the right | a >> n |
Each operator works with integers in binary form, which allows direct manipulation of binary bits.
2. Bitwise AND Operator (&)
The & operator performs a binary AND operation on each pair of corresponding bits in the operands.
The result is 1 only if both bits are 1; otherwise, it is 0.
Example: Bitwise AND
#include <stdio.h> int main() { int a = 12; // Binary: 1100 int b = 10; // Binary: 1010 int result = a & b; // Result: 1000 (Binary) = 8 (Decimal) printf("Result of %d & %d = %d\n", a, b, result); return 0; }
Explanation:
- 12 in binary is 1100, and 10 in binary is 1010.
- The AND operation results in 1000 (binary), which is 8 in decimal.
Output:
Result of 12 & 10 = 8
3. Bitwise OR Operator (|)
The | operator performs a binary OR operation on each pair of bits. The result is 1 if at least one of the bits is 1; otherwise, it is 0.
Example: Bitwise OR
#include <stdio.h> int main() { int a = 12; // Binary: 1100 int b = 10; // Binary: 1010 int result = a | b; // Result: 1110 (Binary) = 14 (Decimal) printf("Result of %d | %d = %d\n", a, b, result); return 0; }
Explanation:
- 12 in binary is 1100, and 10 in binary is 1010.
- The OR operation results in 1110 (binary), which is 14 in decimal.
Output:
Result of 12 | 10 = 14
4. Bitwise XOR Operator (^)
The ^ operator performs a binary XOR (exclusive OR) operation on each pair of bits. The result is 1 only if exactly one of the bits is 1; otherwise, it is 0.
Example: Bitwise XOR
#include <stdio.h> int main() { int a = 12; // Binary: 1100 int b = 10; // Binary: 1010 int result = a ^ b; // Result: 0110 (Binary) = 6 (Decimal) printf("Result of %d ^ %d = %d\n", a, b, result); return 0; }
Explanation:
- 12 in binary is 1100, and 10 in binary is 1010.
- The XOR operation results in 0110 (binary), which is 6 in decimal.
Output:
Result of 12 ^ 10 = 6
5. Bitwise NOT Operator (~)
The ~ operator performs a unary NOT operation, which inverts each bit. All 1s become 0s, and all 0s become 1s.
Example: Bitwise NOT
#include <stdio.h> int main() { int a = 12; // Binary: 00000000 00000000 00000000 00001100 (for 32-bit int) int result = ~a; // Result: 11111111 11111111 11111111 11110011 printf("Result of ~%d = %d\n", a, result); return 0; }
Explanation:
- Inverting 12 (binary 00000000 00000000 00000000 00001100) results in all bits flipped, often interpreted as a large negative number due to two’s complement representation.
Output (on most 32-bit systems):
Result of ~12 = -13
6. Left Shift Operator (<<)
The << operator shifts all bits in a number to the left by a specified number of positions. Each left shift by 1 effectively multiplies the number by 2.
Example: Left Shift
#include <stdio.h> int main() { int a = 3; // Binary: 0011 int result = a << 2; // Result: 1100 (Binary) = 12 (Decimal) printf("Result of %d << 2 = %d\n", a, result); return 0; }
Explanation:
- Shifting 3 (0011 in binary) to the left by 2 results in 1100 (binary), which is 12 in decimal.
Output:
Result of 3 << 2 = 12
7. Right Shift Operator (>>)
The >> operator shifts all bits in a number to the right by a specified number of positions. Each right shift by 1 divides the number by 2.
Example: Right Shift
#include <stdio.h> int main() { int a = 12; // Binary: 1100 int result = a >> 2; // Result: 0011 (Binary) = 3 (Decimal) printf("Result of %d >> 2 = %d\n", a, result); return 0; }
Explanation:
- Shifting 12 (1100 in binary) to the right by 2 results in 0011 (binary), which is 3 in decimal.
Output:
Result of 12 >> 2 = 3
8. Using Bitwise Operators in Bit Masking
Bit masking is a technique where specific bits are extracted or modified using bitwise operators. This can be useful for extracting or setting specific bits in a number.
Example: Checking if a Specific Bit is Set
To check if a specific bit (like the 3rd bit) is set, use the AND operation with a bitmask.
#include <stdio.h> int main() { int num = 5; // Binary: 0101 int bitmask = 1 << 2; // Binary: 0100 (only the 3rd bit is set) if (num & bitmask) { printf("The 3rd bit is set.\n"); } else { printf("The 3rd bit is not set.\n"); } return 0; }
Explanation:
- 1 << 2 creates a bitmask 0100, which represents the 3rd bit.
- Using num & bitmask checks if the 3rd bit in num is 1.
Output:
The 3rd bit is not set.
9. Setting and Clearing Specific Bits
Using bitwise operators, you can set or clear specific bits in a number.
Example: Setting the 2nd Bit of a Number
#include <stdio.h> int main() { int num = 5; // Binary: 0101 int bitmask = 1 << 1; // Binary: 0010 (2nd bit) num = num | bitmask; // Sets the 2nd bit printf("Number after setting 2nd bit: %d\n", num); // Output: 7 return 0; }
Explanation:
- The OR operation num | bitmask sets the 2nd bit of num.
Output:
Number after setting 2nd bit: 7
Example: Clearing the 3rd Bit of a Number
#include <stdio.h> int main() { int num = 7; // Binary: 0111 int bitmask = ~(1 << 2); // Binary: 1011 (Clears the 3rd bit) num = num & bitmask; // Clears the 3rd bit printf("Number after clearing 3rd bit: %d\n", num); // Output: 3 return 0; } `` ` Explanation: - `~(1 << 2)` creates a bitmask where only the 3rd bit is `0`. - The AND operation `num & bitmask` clears the 3rd bit in `num`. #### Output:
Number after clearing 3rd bit: 3
### Summary Table of Bitwise Operators | Operator | Description | Example | |----------|----------------------------------------------|------------------------------| | `&` | AND - Sets each bit to 1 if both bits are 1 | `a & b` | | `|` | OR - Sets each bit to 1 if at least one bit is 1 | `a | b` | | `^` | XOR - Sets each bit to 1 if only one bit is 1 | `a ^ b` | | `~` | NOT - Inverts all bits | `~a` | | `<<` | Left Shift - Shifts bits left, fills with 0s | `a << n` | | `>>` | Right Shift - Shifts bits right, fills with sign bit for signed numbers or 0 for unsigned | `a >> n` | ### Complete Example Program: Bitwise Manipulation This program uses bitwise operations to check, set, and clear specific bits of an integer. ```c #include <stdio.h> void checkBit(int num, int pos) { if (num & (1 << pos)) printf("Bit %d is set in %d\n", pos, num); else printf("Bit %d is not set in %d\n", pos, num); } int main() { int num = 5; // Binary: 0101 printf("Original number: %d\n", num); checkBit(num, 1); // Check if 2nd bit is set checkBit(num, 2); // Check if 3rd bit is set // Set the 3rd bit num = num | (1 << 2); printf("Number after setting 3rd bit: %d\n", num); checkBit(num, 2); // Clear the 3rd bit num = num & ~(1 << 2); printf("Number after clearing 3rd bit: %d\n", num); checkBit(num, 2); return 0; }
In this example:
- checkBit function checks if a specific bit in num is set.
- The main program sets and clears specific bits in num.
Output:
Original number: 5 Bit 1 is set in 5 Bit 2 is not set in 5 Number after setting 3rd bit: 7 Bit 2 is set in 7 Number after clearing 3rd bit: 3 Bit 2 is not set in 3
This tutorial covers the basics of bitwise operators in C, demonstrating how to use them for binary manipulation tasks such as masking, setting, and clearing bits.