In C language, bitwise operators are used to perform operations on individual bits of integers. These operators allow direct manipulation of bits, making them useful for tasks such as low-level programming, optimization, and systems programming. Bitwise operations are fast and efficient for various applications like setting, clearing, or flipping particular bits.
List of Bitwise Operators in C
Bitwise AND (
&
):- Performs a logical AND operation on each bit of the operands.
- Resulting bit is
1
only if both bits are1
. - Example:
int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a & b; // 0001 in binary, which is 1
Bitwise OR (
|
):- Performs a logical OR operation on each bit of the operands.
- Resulting bit is
1
if at least one bit is1
. - Example:
int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a | b; // 0111 in binary, which is 7
Bitwise XOR (
^
):- Performs a logical XOR (exclusive OR) operation on each bit of the operands.
- Resulting bit is
1
if one bit is1
and the other is0
. - Example:
int a = 5; // 0101 in binary int b = 3; // 0011 in binary int result = a ^ b; // 0110 in binary, which is 6
Bitwise NOT (
~
):- Inverts all bits of the operand (changes
1
to0
and0
to1
). - This is a unary operator (applies to one operand).
- Example:
int a = 5; // 0101 in binary int result = ~a; // 1010 in binary (in two's complement representation), which is -6
- Inverts all bits of the operand (changes
Left Shift (
<<
):- Shifts bits of the first operand to the left by the number of positions specified by the second operand.
- Equivalent to multiplying by
2^n
. - Example:
int a = 5; // 0101 in binary int result = a << 1; // 1010 in binary, which is 10
Right Shift (
>>
):- Shifts bits of the first operand to the right by the number of positions specified by the second operand.
- Equivalent to dividing by
2^n
(discarding the remainder for integers). - Example:
int a = 20; // 10100 in binary int result = a >> 2; // 00101 in binary, which is 5
Bitwise Operator Examples
Here's a complete example of how to use bitwise operators in a C program:
#include <stdio.h>
int main() {
int a = 6; // 0110 in binary
int b = 3; // 0011 in binary
printf("a & b = %d\n", a & b); // 0010 in binary, which is 2
printf("a | b = %d\n", a | b); // 0111 in binary, which is 7
printf("a ^ b = %d\n", a ^ b); // 0101 in binary, which is 5
printf("~a = %d\n", ~a); // 1001 in binary (in two's complement), which is -7
printf("a << 1 = %d\n", a << 1); // 1100 in binary, which is 12
printf("b >> 1 = %d\n", b >> 1); // 0001 in binary, which is 1
return 0;
}
Summary of Bitwise Operators:
&
(AND): Returns1
if both bits are1
.|
(OR): Returns1
if at least one bit is1
.^
(XOR): Returns1
if one bit is1
and the other is0
.~
(NOT): Inverts all bits.<<
(Left Shift): Shifts bits to the left, effectively multiplying by2^n
.>>
(Right Shift): Shifts bits to the right, effectively dividing by2^n
.
Important Points:
- Efficiency: Bitwise operators are highly efficient, particularly in low-level programming, such as working with hardware registers or network protocols.
- Two's Complement: The
~
operator follows the two's complement representation for negative numbers, which may lead to unexpected results for beginners. - Unsigned vs. Signed: For left and right shifts, be careful with signed integers. The right shift for signed values may be implementation-dependent, depending on whether it's arithmetic or logical shift.
Bitwise operators are powerful tools that allow fine control over individual bits, making them essential for certain types of optimizations and low-level programming tasks.