What is a Bit?
- 0 (OFF)
- 1 (ON)
- 1 Byte = 8 bits
- 5 in binary (8-bit) = 00000101
What is Bit Manipulation?
- Optimizing performance
- Saving memory using bit flags or masks
- Solving mathematical and logical problems more efficiently
Decimal to Binary Conversion.
- Divide the number by 2.
- Record the remainder (it will be 0 or 1).
- Continue dividing the quotient by 2 until it becomes 0.
- The binary number is the reverse order of the remainders.
13 ÷ 2 = 6 remainder 1
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
→ Binary = 1101Binary to Decimal Conversion
- Start from the rightmost bit.
- Multiply each bit by 2^position.
- Sum all the results.
= 1×2³ + 0×2² + 1×2¹ + 0×2⁰
= 8 + 0 + 2 + 0
= 10
Bitwise Operators.
Truth Table of Bitwise AND
| A | B | A & B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
5 = 0101
3 = 0011
5 & 3 = 0001 → 1
Truth Table of Bitwise OR
| A | B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
5 = 0101
3 = 0011
5 | 3 = 0111 → 7
Truth Table of Bitwise XOR
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
5 = 0101
3 = 0011
5 ^ 3 = 0110 → 6
- 0 becomes 1
- 1 becomes 0
~5 = -(5 + 1) = -6
5 = 00000101
~5 = 11111010 (in 8-bit) → which is -6 in 2’s complement
5 << 1 = 10
Binary: 0101 << 1 → 1010
5 >> 1 = 2
Binary: 0101 >> 1 → 0010
What is 2's Complement?
- Write the number in binary (fixed width, e.g., 8 bits).
- Invert all the bits (change 0 to 1 and 1 to 0).
- Add 1 to the result.
- Positive numbers start from 00000000 (0) to 01111111 (+127)
- Negative numbers start from 11111111 (–1) to 10000000 (–128)
Step 1: Binary of 5 in 8 bits = 00000101
Step 2: Invert bits → 11111010
Step 3: Add 1 → 11111011
Result: 11111011 is -5 in 2’s complement form.



Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.