C Bitwise Operators

C Bitwise Operators
C Bitwise Operators in C allow you to manipulate individual bits of integers. They are commonly used in:
✔ Low-level programming
✔ Embedded systems
✔ Network programming
✔ Performance-critical code
✔ Device drivers and microcontrollers
These operators work on data bit-by-bit.
List of Bitwise Operators
| Operator | Meaning | Example |
|---|---|---|
& | Bitwise AND | a & b |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT (complement) | ~a |
<< | Left Shift | a << n |
>> | Right Shift | a >> n |
⭐ Binary Example
Let:
1️⃣ Bitwise AND (&)
2️⃣ Bitwise OR (|)
3️⃣ Bitwise XOR (^)
XOR returns 1 when bits are different.
4️⃣ Bitwise NOT (~)
Flips all bits.
5️⃣ Left Shift (<<)
Shifts bits left, filling zeros.
Equivalent to multiplying by 2^n.
6️⃣ Right Shift (>>)
Shifts bits right.
Equivalent to dividing by 2^n.
🧠 Practical Examples
Checking if a Number is Even or Odd
(Using AND with 1)
Swapping Two Variables Without a Temp
Turning a Specific Bit ON
Turning a Bit OFF
Toggle a Bit
📌 Summary Table
| Operation | Example | Result |
|---|---|---|
| AND | 5 & 3 | 1 |
| OR | `5 | 3` |
| XOR | 5 ^ 3 | 6 |
| NOT | ~5 | -6 |
| Left shift | 5 << 1 | 10 |
| Right shift | 5 >> 1 | 2 |
