Bitwise Operators
Last Updated: Jan 2026
Bitwise operators work on numbers at the binary (bit) level. They treat numbers as a sequence of bits (0s and 1s).
🗂 List of Bitwise Operators
✏ Mini Example
int a = 5; // binary: 0101
int b = 1; // binary: 0001
printf("%d\n",(a & b)); // 1 → (0101 & 0001)
printf("%d\n",(a | b)); // 5 → (0101 | 0001)
printf("%d\n",(a ^ b)); // 4 → (0101 ^ 0001)
printf("%d\n",(~a)); // -6 → (bitwise NOT)
printf("%d\n",(a << 1)); // 10 → (0101 → 1010)
printf("%d\n",(a >> 1)); // 2 → (0101 → 0010)
🧮 Binary Visualization
Bitwise with Compound Operators
int x = 5;
x &= 3; // x = x & 3
printf("%d\n",x); // 1
x |= 2; // x = x | 2
printf("%d\n",x); // 3
x ^= 1; // x = x ^ 1
printf("%d\n",x); // 2
x <<= 1; // x = x << 1
printf("%d\n",x); // 4
💡 Quick Practice
- Find a & b, a | b, and a ^ b for a = 12, b = 5.
- Check what happens for ~7.
- Multiply 6 by 2 using <<.
- Divide 12 by 2 using >>.
- Try bitwise compound: int x = 8; x >>= 2; printf("%d\n",x);
