Bitwise Operators in C Programming





Bitwise operators in C are used to manipulate individual bits of data within integer types. They perform operations at the bit level, allowing you to set, clear, toggle, or check specific bits within variables.

Operators Table

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR (Exclusive OR)
~ Bitwise NOT (Complement)
<< Left shift
>> Right shift


Example

Here's an example of using bitwise operators in C:

#include <stdio.h>

int main() {
unsigned int a = 5; // Binary: 0101
unsigned int b = 3; // Binary: 0011

unsigned int bitwise_and = a & b;
unsigned int bitwise_or = a | b;
unsigned int bitwise_xor = a ^ b;
unsigned int bitwise_not = ~a;
unsigned int left_shift = a << 1; // Shift left by 1 bit
unsigned int right_shift = a >> 1; // Shift right by 1 bit

printf("Bitwise AND: %u\n", bitwise_and);
printf("Bitwise OR: %u\n", bitwise_or);
printf("Bitwise XOR: %u\n", bitwise_xor);
printf("Bitwise NOT: %d\n", bitwise_not);
printf("Left Shift: %u\n", left_shift);
printf("Right Shift: %u\n", right_shift);

return 0;
}
Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left Shift: 10
Right Shift: 2

This example demonstrates various bitwise operations on two unsigned integers (a and b) and displays the results.