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