Bitwise Operators in C Programming

Bitwise operators are fundamental in C programming for manipulating individual bits of data stored in variables. In this article, we’ll delve into the basics of C programming’s bitwise operators: & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Left shift), and >> (Right shift). Through real-world examples and output demonstrations, you’ll gain a solid understanding of how to use these operators effectively in your C programs. Bitwise operators are powerful tools in C programming, enabling you to manipulate individual bits in variables efficiently. In this article, we’ve explored the basic bitwise operators (&, |, ^, ~, <<, and >>) with real-world examples and output demonstrations.

Bitwise AND (&)

The bitwise AND operator (&) performs a bitwise AND operation between the binary representation of two values. It returns a result with set bits only if both corresponding bits of the operands are set. Let’s illustrate this with an example:

#include <stdio.h>
int main() {
int num1 = 12;  // Binary: 1100
int num2 = 10;  // Binary: 1010
int result = num1 & num2;  // Binary: 1000 (Decimal: 8)
printf("Result of num1 & num2: %d\n", result);
return 0;
}


Output:

Result of num1 & num2: 8


Bitwise OR (|)

The bitwise OR operator (|) performs a bitwise OR operation between the binary representation of two values. It returns a result with set bits if at least one corresponding bit of the operands is set. Here’s an example:

#include <stdio.h>
int main() {
int num1 = 12;  // Binary: 1100
int num2 = 10;  // Binary: 1010
int result = num1 | num2;  // Binary: 1110 (Decimal: 14)
printf("Result of num1 | num2: %d\n", result);
return 0;
}


Output:

Result of num1 | num2: 14


Bitwise XOR (^)

The bitwise XOR operator (^) performs a bitwise XOR operation between the binary representation of two values. It returns a result with set bits in positions where only one of the corresponding bits in the operands is set. Let’s see it in action:

#include <stdio.h>
int main() {
int num1 = 12;  // Binary: 1100
int num2 = 10;  // Binary: 1010
int result = num1 ^ num2;  // Binary: 0110 (Decimal: 6)
printf("Result of num1 ^ num2: %d\n", result);
return 0;
}


Output:

Result of num1 ^ num2: 6


Bitwise NOT (~)

The bitwise NOT operator (~) performs a bitwise NOT operation on a single value, flipping all the bits (changing 0s to 1s and vice versa). Here’s an example:

#include <stdio.h>
int main() {
int num = 12;  // Binary: 1100
int result = ~num;  // Binary: 0011 (Decimal: -13)
printf("Result of ~num: %d\n", result);
return 0;
}

Output:

Result of ~num: -13


Left Shift (<<) and Right Shift (>>)

The left shift (<<) and right shift (>>) operators are used to shift the bits of a value to the left or right by a specified number of positions. Let’s explore both operators:

#include <stdio.h>
int main() {
int num = 12;  // Binary: 1100
// Left shift by 2 positions
int leftShiftResult = num << 2;  // Binary: 110000 (Decimal: 48)
// Right shift by 2 positions
int rightShiftResult = num >> 2;  // Binary: 11 (Decimal: 3)
printf("Left shift result: %d\n", leftShiftResult);
printf("Right shift result: %d\n", rightShiftResult);
return 0;
}


Output:

Left shift result: 48
Right shift result: 3

Author: user