🧩 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
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
| & | AND | Sets each bit to 1 if both bits are 1 | 5 & 1 | 1 |
| | | OR | Sets each bit to 1 if any bit is 1 | 5 | 1 | 5 |
| ^ | XOR | Sets each bit to 1 if bits are different | 5 ^ 1 | 4 |
| ~ | NOT | Inverts all bits (1 → 0, 0 → 1) | ~5 | -6 |
| << | Left Shift | Shifts bits left (multiply by 2) | 5 << 1 | 10 |
| >> | Right Shift | Shifts bits right (divide by 2) | 5 >> 1 | 2 |
✏ 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
| Operation | Binary Form | Result |
|---|---|---|
| 5 & 1 | 0101 & 0001 | 0001 (1) |
| 5 | 1 | 0101 | 0001 | 0101 (5) |
| 5 ^ 1 | 0101 ^ 0001 | 0100 (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
- 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);