# Number Systems

*Bismillahir Rahmanir Rahim*

# Understanding Number Systems: Decimal, Binary, and Hexadecimal

Nice to meet you again! While you are likely accustomed to working with everyday decimal numbers, digital systems rely heavily on **Binary** (0s and 1s) and **Hexadecimal** systems. This is primarily due to their convenience and efficiency in hardware design and programming.

Let's break down how these systems work and how to seamlessly convert between them.

* * *

## 1\. Decimal Digits (Base 10)

The decimal system uses ten digits: **0, 1, 2, 3, 4, 5, 6, 7, 8, and 9**.

Each column represents a value that is 10 times more than the previous column (reading from right to left). These are also called **Base 10** numbers because we represent each column as a multiple of a power of 10.

*   **Capacity:** An `N`\-digit decimal number can represent up to `10ᴺ` possibilities.
    
*   **Example:** A 3-digit number has `10³ = 1000` possibilities (ranging from `000` to `999`).
    

* * *

## 2\. Binary Numbers (Base 2)

In the binary system, "bits" represent only one of two values: **0 or 1**. Each column carries twice the weight of the previous column, making this a **Base 2** system.

### The Powers of 2 Table

If you work in programming or IT, you will save significant time by memorizing the powers of 2 up to `2¹⁶`:

| Power | Value |  | Power | Value |
| --- | --- | --- | --- | --- |
| `2⁰` | 1 |  | `2⁹` | 512 |
| `2¹` | 2 |  | `2¹⁰` | 1024 (1k) |
| `2²` | 4 |  | `2¹¹` | 2048 |
| `2³` | 8 |  | `2¹²` | 4096 |
| `2⁴` | 16 |  | `2¹³` | 8192 |
| `2⁵` | 32 |  | `2¹⁴` | 16384 |
| `2⁶` | 64 |  | `2¹⁵` | 32768 |
| `2⁷` | 128 |  | `2¹⁶` | 65536 |
| `2⁸` | 256 |  |  |  |

> **Converting Binary to Decimal:** Let's convert `1101` (binary) to decimal: = `(1 * 2³) + (1 * 2²) + (0 * 2¹) + (1 * 2⁰)` = `8 + 4 + 0 + 1` = `13` (decimal)

* * *

## 3\. Hexadecimal Numbers (Base 16)

Binary numbers can become very long and prone to errors when written out. To simplify this, we use Hexadecimal (**Base 16**).

A group of 4 bits gives us 16 possibilities (`2⁴ = 16`). With just **one** hexadecimal digit, we can represent an entire 4-bit sequence!

### Hexadecimal Conversion Table

| Decimal | Hex | Binary |  | Decimal | Hex | Binary |
| --- | --- | --- | --- | --- | --- | --- |
| **0** | `0` | `0000` |  | **8** | `8` | `1000` |
| **1** | `1` | `0001` |  | **9** | `9` | `1001` |
| **2** | `2` | `0010` |  | **10** | `A` | `1010` |
| **3** | `3` | `0011` |  | **11** | `B` | `1011` |
| **4** | `4` | `0100` |  | **12** | `C` | `1100` |
| **5** | `5` | `0101` |  | **13** | `D` | `1101` |
| **6** | `6` | `0110` |  | **14** | `E` | `1110` |
| **7** | `7` | `0111` |  | **15** | `F` | `1111` |

* * *

## 4\. Practical Conversions

Here is how you can practically convert numbers between these three systems.

### A. Hexadecimal to Binary and Decimal

**Goal: Convert** `2ED` **(hex) to binary and decimal.**

*   **Hex to Binary:** Map each character using the table above.
    
    *   `2` = `0010`
        
    *   `E` = `1110`
        
    *   `D` = `1101`
        
    *   **Result:** `001011101101` (binary)
        
*   **Hex to Decimal:** Multiply by powers of 16.
    
    *   \= `(2 * 16²) + (14 * 16¹) + (13 * 16⁰)`
        
    *   \= `512 + 224 + 13`
        
    *   **Result:** `749` (decimal)
        

### B. Binary to Hexadecimal

**Goal: Convert** `1111010` **(binary) to hexadecimal.**

1.  **Group into 4 bits:** Start from the right. Pad with leading zeros if necessary: `0111` and `1010`.
    
2.  **Map to Hex:** \* `1010` = `A`
    
    *   `0111` = `7`
        
3.  **Result:** `7A` (hex)
    

### C. Decimal to Hexadecimal and Binary

**Goal: Convert** `333` **(decimal) to hexadecimal and binary.**

There are two primary methods for doing this: the Subtraction Method and the Repeated Division Method.

#### Method 1: The Subtraction Method (Descending Powers)

You subtract the largest possible power of the target base until you reach zero.

*   **Decimal to Hexadecimal:**
    
    1.  The largest power of 16 less than 333 is **256** (which is `16²`).
        
    2.  256 goes into 333 **once** (`1 * 256`). *Remainder:* `333 - 256 = 77`.
        
    3.  16 goes into 77 **four** times (`4 * 16 = 64`). *Remainder:* `77 - 64 = 13`.
        
    4.  13 in decimal is **D** in hex.
        
    5.  **Hex Result:** `14D` (hex)
        
*   **Decimal to Binary:**
    
    1.  `333 - 256` (which is `2⁸`) = **77**
        
    2.  `77 - 64` (which is `2⁶`) = **13**
        
    3.  `13 - 8` (which is `2³`) = **5**
        
    4.  `5 - 4` (which is `2²`) = **1**
        
    5.  `1 - 1` (which is `2⁰`) = **0**
        
    
    *   *Since binary digits exist at the positions for* `2⁸`*,* `2⁶`*,* `2³`*,* `2²`*, and* `2⁰`*, we place a* `1` *in those columns and a* `0` *everywhere else.*
        
    *   **Binary Result:** `101001101`
        

#### Method 2: Repeated Division (The Remainder Method)

This is often the easiest method for programming or working with large numbers. You repeatedly divide the decimal number by the target base (2 or 16) and record the remainders. **Read the remainders from bottom to top** to get your answer.

*   **Decimal to Hexadecimal (Divide by 16):**
    
    1.  `333 ÷ 16` = 20, remainder **13** *(which is Hex* ***D****)*
        
    2.  `20 ÷ 16` = 1, remainder **4** *(which is Hex* ***4****)*
        
    3.  `1 ÷ 16` = 0, remainder **1** *(which is Hex* ***1****)*
        
    
    *   **Read bottom to top Result:** `14D` (hex)
        
*   **Decimal to Binary (Divide by 2):**
    
    1.  `333 ÷ 2` = 166, remainder **1**
        
    2.  `166 ÷ 2` = 83, remainder **0**
        
    3.  `83 ÷ 2` = 41, remainder **1**
        
    4.  `41 ÷ 2` = 20, remainder **1**
        
    5.  `20 ÷ 2` = 10, remainder **0**
        
    6.  `10 ÷ 2` = 5, remainder **0**
        
    7.  `5 ÷ 2` = 2, remainder **1**
        
    8.  `2 ÷ 2` = 1, remainder **0**
        
    9.  `1 ÷ 2` = 0, remainder **1**
        
    
    *   **Read bottom to top Result:** `101001101` (binary)
