Bitwise Operators in C: A Guide to Low-Level Manipulation
Delve into the world of bitwise operators in C, a powerful tool for performing operations at the bit level. Understand the various bitwise operators, including AND, OR, XOR, NOT, left shift, and right shift, and their applications in tasks such as bit masking, bit counting, and data compression. This guide will equip you with the knowledge to leverage bitwise operators for efficient and optimized C programming.
Bitwise Operators in C
Bitwise operators in C allow low-level manipulation of data stored in the computer's memory. Unlike logical operators that work on Boolean values, bitwise operators work on individual bits of integer types.
Bitwise Operators
The following table shows the bitwise operators in C:
Operator | Description | Example |
---|---|---|
& | Binary AND Operator. Copies a bit to the result if it exists in both operands. | (A & B) |
| | Binary OR Operator. Copies a bit if it exists in either operand. | (A | B) |
^ | Binary XOR Operator. Copies the bit if it is set in one operand but not both. | (A ^ B) |
~ | Binary One's Complement Operator. Unary operator that 'flips' bits. | (~A) |
<< | Binary Left Shift Operator. Moves the left operand's value left by the number of bits specified by the right operand. | (A << 2) |
>> | Binary Right Shift Operator. Moves the left operand's value right by the number of bits specified by the right operand. | (A >> 2) |
Bitwise AND (&) Operator
The bitwise AND (&) operator performs according to the following truth table:
bit a | bit b | a & b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Assuming the variables a
and b
have values 60 (0011 1100 in binary) and 13 (0000 1101 in binary), the a & b
operation results in 12 (0000 1100 in binary).
Bitwise OR (|) Operator
The bitwise OR (|) operator performs according to the following truth table:
bit a | bit b | a | b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Assuming the variables a
and b
have values 60 (0011 1100 in binary) and 13 (0000 1101 in binary), the a | b
operation results in 61 (0011 1101 in binary).
Bitwise XOR (^) Operator
The bitwise XOR (^) operator performs according to the following truth table:
bit a | bit b | a ^ b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Assuming the variables a
and b
have values 60 (0011 1100 in binary) and 13 (0000 1101 in binary), the a ^ b
operation results in 49 (0011 0001 in binary).
Left Shift (<<) Operator
The left shift operator shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. Blank spaces are filled with zeroes.
Assuming the variable a
has the value 60 (0011 1100 in binary), the a << 2
operation results in 240 (1111 0000 in binary).
Right Shift (>>) Operator
The right shift operator shifts each bit in its left-hand operand to the right by the number of positions indicated by the right-hand operand. Blank spaces are filled with zeroes.
Assuming the variable a
has the value 60 (0011 1100 in binary), the a >> 2
operation results in 15 (0000 1111 in binary).
One's Complement (~) Operator
The one's complement operator (~) is a unary operator that flips the bits of its operand. A bit that is 1 is changed to 0 and vice versa.
Assuming the variable a
has the value 60 (0011 1100 in binary), the ~a
operation results in -61 (1100 0011 in binary, using two's complement form).
Example
The following example highlights the operation of all the bitwise operators:
Code
#include <stdio.h>
int main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /* -61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
return 0;
}
Output
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15