⚡ Bitwise Operators

Last Updated: 16th August 2025


Bitwise operators work at the binary level (0s and 1s).
They directly operate on the bits of integers.

Hinglish Tip 🗣: Ye operators number ke bits par kaam karte hain (0-1 ke form me).


OperatorNameMeaningExampleResult
&AND1 if both bits are 16 & 32
|OR1 if at least one bit is 16 | 37
^XOR1 if bits are different6 ^ 35
~NOTInverts all bits~6-7
<<Left ShiftShift bits to left, fills 0 on right6 << 112
>>Right ShiftShift bits to right6 >> 13

✏ Syntax & Examples

a = 6   # binary: 110
b = 3   # binary: 011

print(a & b)   # 2  -> 010
print(a | b)   # 7  -> 111
print(a ^ b)   # 5  -> 101
print(~a)      # -7 -> two's complement
print(a << 1)  # 12 -> 1100
print(a >> 1)  # 3  -> 11

💡 Quick Practice

  • Calculate 12 & 5 (hint: 12 = 1100, 5 = 0101).
  • Try 7 | 4 and predict output.
  • Check 9 ^ 5 → kya hoga?
  • What will ~10 return?
  • Use << to multiply 3 by 8 using shifting.
  • Use >> to divide 40 by 4 using shifting.
  • Exercises