🧩 Bitwise Operators

Last Updated: 11th October 2025


Bitwise operators work on numbers at the binary (bit) level.
They treat numbers as a sequence of bits (0s and 1s).

Hinglish Tip 🗣: Ye operator number ke “bits” (0 aur 1) ke sath kaam karte hain — jaise computer ke level par math hoti hai.


🗂 List of Bitwise Operators

OperatorNameDescriptionExampleResult
&ANDSets each bit to 1 if both bits are 15 & 11
|ORSets each bit to 1 if any bit is 15 | 15
^XORSets each bit to 1 if bits are different5 ^ 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

let a = 5; // binary: 0101
let b = 1; // binary: 0001

console.log(a & b); // 1  → (0101 & 0001)
console.log(a | b); // 5  → (0101 | 0001)
console.log(a ^ b); // 4  → (0101 ^ 0001)
console.log(~a); // -6 → (bitwise NOT)
console.log(a << 1); // 10 → (0101 → 1010)
console.log(a >> 1); // 2  → (0101 → 0010)

Hinglish Tip 🗣: Left shift (<<) number ko 2 se multiply karta hai, Right shift (>>) number ko 2 se divide karta hai (integer result).


🧮 Binary Visualization

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

⚙️ Bitwise with Compound Operators

let x = 5;
x &= 3; // x = x & 3
console.log(x); // 1

x |= 2; // x = x | 2
console.log(x); // 3

x ^= 1; // x = x ^ 1
console.log(x); // 2

x <<= 1; // x = x << 1
console.log(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: let x = 8; x >>= 2; console.log(x);