#include <stdio.h>
a = a ^ b;
b = a ^ b;
a = a ^ b;
Check if a Number is Even or Odd:
if (num & 1) {
// odd
} else {
// even
}
Count Set Bits (1s) in an Integer:
int count = 0;
while (num) {
count += num & 1;
num >>= 1;
}
Toggle the Nth Bit of a Number:
num = num ^ (1 << n);
Multiply a Number by 2 using Bitwise Operations:
num = num << 1;
Divide a Number by 2 using Bitwise Operations:
num = num >> 1;
Swap Adjacent Bits:
num = ((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1);
Check if a Number is a Palindrome in Binary Representation:
int isBinaryPalindrome(int num) {
int reversed = 0, original = num;
while (num) {
reversed = (reversed << 1) | (num & 1);
num >>= 1;
}
return original == reversed;
}
Reverse Bits:
uint32_t reverseBits(uint32_t num) {
uint32_t result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (num & 1);
num >>= 1;
}
return result;
}
Count Different Bits in Two Numbers:
int hammingDistance(int x, int y) {
int xor_result = x ^ y;
int count = 0;
while (xor_result) {
count += xor_result & 1;
xor_result >>= 1;
}
return count;
}
Swap Odd and Even Bits:
unsigned int swapOddEvenBits(unsigned int x) {
return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: