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

OperatorNameDescriptionExampleResult
&AND

Sets each bit to 1 if both bits are 1

5 & 11
|OR

Sets each bit to 1 if any bit is 1

5 | 15
^XOR

Sets each bit to 1 if bits are different

5 ^ 14
~NOTInverts all bits (1 → 0, 0 → 1)~5-6
<<Left ShiftShifts bits left (multiply by 2)5 << 110
>>Right ShiftShifts bits right (divide by 2)5 >> 12

✏ 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

OperationBinary FormResult
5 & 10101 & 00010001 (1)
5 | 10101 | 00010101 (5)
5 ^ 10101 ^ 00010100 (4)

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

  1. Find a & b, a | b, and a ^ b for a = 12, b = 5.
  2. Check what happens for ~7.
  3. Multiply 6 by 2 using <<.
  4. Divide 12 by 2 using >>.
  5. Try bitwise compound: int x = 8; x >>= 2; printf("%d\n",x);

Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR