Bitwise operations are a way to directly work with the binary digits (bits) of a number.
In C++, these operations use specific symbols to manipulate bits in various ways. Let’s explore these operations and some cool tricks you can do with them!
Basic Bitwise Operators
- AND (
&
): Compares each bit of two numbers and returns a new number whose bits are 1 if both bits are 1, and 0 otherwise.
Example:
int result = 5 & 3; // result will be 1 (0000 0101 & 0000 0011 = 0000 0001)
2. OR (|
): Compares each bit of two numbers and returns a new number whose bits are 1 if at least one of the bits is 1.
Example:
int result = 5 | 3; // result will be 7 (0000 0101 | 0000 0011 = 0000 0111)
3. XOR (^
): Compares each bit of two numbers and returns a new number whose bits are 1 if the bits are different.
Example:
int result = 5 ^ 3; // result will be 6 (0000 0101 ^ 0000 0011 = 0000 0110)
4. NOT (~
): Flips all the bits of a number (0s become 1s and 1s become 0s).
Example:
int result = ~5; // result will be -6 (1111 1010)
5. Left Shift (<<
): Shifts all bits of a number to the left by a certain number of positions.
Example:
int result = 5 << 1; // result will be 10 (0000 0101 << 1 = 0000 1010)
6. Right Shift (>>
): Shifts all bits of a number to the right by a certain number of positions.
Example:
int result = 5 >> 1; // result will be 2 (0000 0101 >> 1 = 0000 0010)
Cool Bitwise Tricks
- Swapping Two Numbers Without a Temporary Variable
void swap(int &a, int &b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
This trick uses the XOR operator to swap the values of two variables without needing a third variable.
2. Checking if a Number is Even or Odd
bool isEven(int x) {
return (x & 1) == 0;
}
If the least significant bit (rightmost bit) of a number is 1, the number is odd; if it’s 0, the number is even.
3. Multiplying and Dividing by Powers of Two
int multiplyBy8(int x) {
return x << 3; // Same as x * 8
}
int divideBy8(int x) {
return x >> 3; // Same as x / 8
}
4. Checking if a Number is a Power of Two
bool isPowerOfTwo(int x) {
return x > 0 && (x & (x - 1)) == 0;
}
A number is a power of two if it has exactly one bit set to 1. This trick checks that condition.
5. Counting the Number of Set Bits (1s)
int countSetBits(int x) {
int count = 0;
while (x) {
x &= (x - 1); // This removes the lowest set bit
count++;
}
return count;
}
This trick efficiently counts how many bits are set to 1 in a number.
6. Finding the Only Non-Repeating Element in an Array
unsigned int reverseBits(unsigned int num) {
unsigned int NO_OF_BITS = sizeof(num) * 8;
unsigned int reverse_num = 0;
for (int i = 0; i < NO_OF_BITS; i++) {
if ((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
If every element in an array appears twice except for one, this trick finds that single element using XOR.
7. Reversing Bits of a Number
unsigned int reverseBits(unsigned int num) {
unsigned int NO_OF_BITS = sizeof(num) * 8;
unsigned int reverse_num = 0;
for (int i = 0; i < NO_OF_BITS; i++) {
if ((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
8. Finding the Most Significant Bit (MSB) Position
int msbPosition(int x) {
int msb = -1;
while (x) {
x = x >> 1;
msb++;
}
return msb;
}
Putting It All Together
Here’s a simple program that demonstrates some of these tricks:
#include <iostream>
void bitwiseTricksExample() {
int a = 5, b = 9;
// Swapping
std::cout << "Before swapping: a = " << a << ", b = " << b << std::endl;
a = a ^ b;
b = a ^ b;
a = a ^ b;
std::cout << "After swapping: a = " << a << ", b = " << b << std::endl;
// Checking even/odd
std::cout << "5 is " << (a & 1 ? "Odd" : "Even") << std::endl;
// Multiplying by 2^3 (8)
int x = 3;
std::cout << "3 * 8 = " << (x << 3) << std::endl;
// Counting set bits
int y = 15; // 1111 in binary
std::cout << "Number of set bits in 15: " << countSetBits(y) << std::endl;
}
int countSetBits(int x) {
int count = 0;
while (x) {
x &= (x - 1); // This removes the lowest set bit
count++;
}
return count;
}
int main() {
bitwiseTricksExample();
return 0;
}
Tags:
Bitwise operators in C++ with example program
Bitwise operators in C++ w3schools
Bitwise operators in C examples
Bitwise operator example
Bitwise XOR C++
Bitwise operators in C Program
Bitwise operator in Java
Bitwise operator in Python