Jan 24, 2026
## Chapter 5: How Computers "Think" (Binary and Hex)
Students often ask: "Sir, I want to be a coder, not a mathematician. Why learn binary?"
The answer is: Because your computer is actually just a collection of millions of tiny light switches. A switch is either ON (1) or OFF (0). That's why it uses **Binary (Base 2)**.
### How to read Binary:
In our normal life (Decimal), we use 10 digits (0-9). In Binary, we only use 2.
The positions are powers of 2:
`8 4 2 1`
So, `1011` in binary is `1*8 + 0*4 + 1*2 + 1*1 = 11` in decimal.
### Examples for Chapter 5:
**1. Basic (Decimal to Binary):**
How to find 13 in binary? Keep dividing by 2 and write the remainders from bottom to top.
```text
13 / 2 = 6 (rem 1)
6 / 2 = 3 (rem 0)
3 / 2 = 1 (rem 1)
1 / 2 = 0 (rem 1)
Answer: 1101
```
**2. Common Mistake (The "Two's Complement" confusion):**
A student sees `-1` in binary and expects it to be `1` with a minus sign.
But in Java, `-1` is actually `11111111...` (all 1s).
*Why:* Java uses "Two's Complement" to store negative numbers. It makes addition and subtraction much faster for the CPU. Don't worry about the full math now, just know that negative numbers look "inverted".
**3. Explanation-Focused (Why Hexadecimal?):**
Binary is too long for humans. `111110101100` is hard to read.
So we use **Hex (Base 16)**. One Hex digit represents 4 bits.
`1111` is `F`, `1010` is `A`. So `11111010` becomes `FA`.
*Analogy:* Binary is like spelling out a word letter-by-letter. Hex is like using shorthand.
**4. Real-Life (Colors in Web):**
Have you seen color codes like `#FF5733`?
That's Hex! `FF` is the amount of Red, `57` is Green, and `33` is Blue.
Each is a number from 0 to 255 (which is the max for 8 bits).
**5. Future Hint (Bitwise Operators):**
In Chapter 6, we'll see operators like `&` and `|`. These are used to manipulate individual switches (bits).
*Teacher's comment:* In high-end gaming or crypto code, we use these to make things 100x faster by bypassing normal math.