# Number Systems: Binary, Decimal, Octal, Hexadecimal

Zvonimir Tomesic, Co-founder, CEO at Codilas · 2023-10-30

> Dive into the world of number systems, exploring the intricacies and practical applications of binary, decimal, octal, and hexadecimal conversions in computing and digital technology.

Source: https://codilas.com/blog/number-systems/

Series: From Binary to Processors: A Deep Dive into Digital Logic

- [Binary 101: The Language Of Computers](/blog/binary-101/)

- Number Systems: Binary, Decimal, Octal, Hexadecimal

- Boolean Algebra: The Foundation Of Digital Logic (coming soon)

- Logic Gates: The Building Blocks Of Digital Logic (coming soon)

- Error Correction In Digital Systems: Understanding Hamming Code And Beyond (coming soon)

- Digital Arithmetic Circuits: From Basic Adders To Complex Chips (coming soon)

- The Anatomy Of Processors: ALU, Clock, And Core Components (coming soon)

## Overview

Understanding various number systems like binary, decimal, octal, and hexadecimal is crucial in computing. These systems each have unique advantages for data processing, representation, and utility in computer environments.

## Grasping the Hexadecimal System

### The Problem with Binary

Binary, though fundamental to computer operations, is verbose and lacks readability, making it challenging to manage and error-prone for humans.

### Why Hexadecimal?

Hexadecimal (base-16) provides a more user-friendly way to represent binary data. Its advantages include:

- **Familiarity**: Hexadecimal digits partially resemble our conventional decimal (base-10) system.
- **Density**: Hexadecimal is denser than binary. For instance, the number 255 can be represented as FF in hexadecimal but requires 8 bits (11111111) in binary.

Hexadecimal is particularly useful in contexts where binary digits need to be concise yet readable. For instance, in memory addressing, color coding in web design (e.g., #FF5733), and debugging computer programs, hexadecimal provides a compact, comprehensible representation.

**Example:**
`e7` in hexadecimal translates to `1110 0111` in binary, equivalent to `231` in decimal.

### Binary-Hexadecimal Compatibility

This compatibility simplifies conversions and is one key reason hexadecimal is preferred in certain applications over decimal. Each hexadecimal digit directly translates to a four-bit binary sequence.

#### Hexadecimal to Binary and Decimal Conversion Table

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

### Converting Hexadecimal to Decimal

To convert hexadecimal to decimal, multiply each digit by 16 raised to the digit's position and sum them up.

**Example:**

Converting `e7` in hexadecimal to decimal:

```
e7
=>
14 * 16 ^ 1 + 7 * 16 ^ 0
= 224 + 7
= 231
```

## Exploring the Octal System

### The Octal Basics

The octal system (base-8) groups binary numbers into sets of three bits, offering another compact representation.

Octal is especially relevant in the world of computing for its simplicity in converting to and from binary. Historically, it was used in older computer systems and remains useful in modern programming for permissions in Unix and Linux file systems (e.g., chmod 777 for full permissions), and in representing digital displays in some electronic devices.

**Example:**

Compare the first 15 numbers in decimal, binary, and octal:

| Decimal | Binary | Octal |
|---------|--------|-------|
| 0       | 0000   | 0     |
| 1       | 0001   | 1     |
| 2       | 0010   | 2     |
| 3       | 0011   | 3     |
| 4       | 0100   | 4     |
| 5       | 0101   | 5     |
| 6       | 0110   | 6     |
| 7       | 0111   | 7     |
| 8       | 1000   | 10    |
| 9       | 1001   | 11    |
| 10      | 1010   | 12    |
| 11      | 1011   | 13    |
| 12      | 1100   | 14    |
| 13      | 1101   | 15    |
| 14      | 1110   | 16    |
| 15      | 1111   | 17    |

### Octal to Binary Conversion

To convert the decimal number 15 to octal, we group its binary form (1111) into `001` and `111`, resulting in `17` in base 8.

**First Group**

| Divide | Quotation | Reminder |
|--------|-----------|----------|
| 2/2    | 1         | 0        |
|--------|-----------|----------|
| 1/2    | 0         | 1        |

If we read **Reminder** from bottom up, we can see `10` as a binary representation of number 2, because we are talking about **octal** we know that octal numbers are created from groups of three bits, we will add zero to the end. So we end up with `010`.

**Second group**

| Divide | Quotation | Reminder |
|--------|-----------|----------|
| 4/2    | 2         | 0        |
|--------|-----------|----------|
| 2/2    | 1         | 0        |
|--------|-----------|----------|
| 1/2    | 0         | 1        |

If we read **Reminder** from bottom up, we can see `100` as a binary representation of number 4.

Then we just concatenate two binary numbers starting from the first group to get the result `010100`.

## Binary Conversions

### Binary to Decimal

For converting binary to decimal, remember that each bit represents a power of two, with the least significant bit (LSB) being \(2^0\).

**Example:**
To convert `010100` from binary to decimal:

```
010100
= 0 * 2^5 + 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0
= 0 + 16 + 0 + 4 + 0 + 0
= 20
```

| Binary | Value | Decimal |
|-------|--------|---------|
| 0     | 1      | 0       |
| 0     | 2      | 0       |
| 1     | 4      | 4       |
| 0     | 8      | 0       |
| 1     | 16     | 16      |
| 0     | 32     | 0       |
| Total |        | 20      |

### Binary to Octal

Convert binary to octal by dividing the binary number into groups of three, starting from the right.

**Example:**
To convert `010100` (20 in decimal) to octal:

```
010100 => 010 | 100
= 2 (octal) | 4 (octal)
= 24 (base 8)
```

**First group**

| binary | value | decimal |
|-------|--------|---------|
| 0     | 1      | 0       |
| 1     | 2      | 2       |
| 0     | 4      | 0       |
| total |        | 2       |

**Second group**

| binary | value | decimal |
|-------|--------|---------|
| 0     | 1      | 0       |
| 0     | 2      | 0       |
| 1     | 4      | 4       |
| total |        | 4       |

Then we just concat total starting from first group `24`

### Binary to Hexadecimal

For binary to hexadecimal, divide the binary number into groups of four.

**Example:**
`010100` (20 in decimal) converts to `14` in hexadecimal.

**First group**

| binary | value | decimal |
|-------|--------|---------|
| 1     | 1      | 1       |
| 0     | 2      | 0       |
| 0     | 4      | 0       |
| 0     | 4      | 0       |
| total |        | 1       |

**Second group**

| binary | value | decimal |
|-------|--------|---------|
| 0     | 1      | 0       |
| 0     | 2      | 0       |
| 1     | 4      | 4       |
| 0     | 8      | 0       |
| total |        | 4       |

Then we just concat total starting from first group `14`

## Octal Conversions

### Octal to Binary

To convert from octal to binary, convert each octal digit into a three-bit binary number.

**Example:**
Octal `24` converts to binary `010100`.

**First Group**

| Divide | Quotation | Reminder |
|--------|-----------|----------|
| 2/2    | 1         | 0        |
|--------|-----------|----------|
| 1/2    | 0         | 1        |

If we read **Reminder** from bottom up, we can see `10` as a binary representation of number 2, because we are talking about **octal** we know that octal numbers are created from groups of three bits, we will add zero to the end. So we end up with `010`.

**Second group**

| Divide | Quotation | Reminder |
|--------|-----------|----------|
| 4/2    | 2         | 0        |
|--------|-----------|----------|
| 2/2    | 1         | 0        |
|--------|-----------|----------|
| 1/2    | 0         | 1        |

If we read **Reminder** from bottom up, we can see `100` as a binary representation of number 4.

Then we just concatenate two binary numbers starting from the first group to get the result `010100`.

### Octal to Decimal

To convert from octal to decimal, multiply each digit by 8 raised to the power of its position and sum the results.

**Example:**
```
24 (octal)
= 4 * 8^0 + 2 * 8^1
= 4 + 16
= 20 (decimal)
```

### Octal to Hexadecimal

For converting from octal to hexadecimal, first convert to binary, then group into fours and convert to hexadecimal.

**Example:**
Octal `24` becomes `14` in hexadecimal.

```

20 (Base 10) => 24(base 8) => 14 (Base 16)

```

The first step is to convert the number to binary (check section **Octal to Binary**) and then convert binary to hexadecimal

After converting number to binary, we can proceed to hexadecimal conversion.

```

  010100 => 0001 | 0100

  0001
  => 1*2^0 + 0*2^1 + 0*2^2 + 0*2^3
   = 1 + 0 + 0 + 0
   = 1

  0100
  => 0*2^0 + 0*2^1 + 1*2^2 + 0*2^3
   = 0 + 0 + 4 + 0
   = 4

  00010100 (base 2) = 14 (base 16)

```

## Conclusion

Understanding these number systems is fundamental in computing and digital electronics. Each system has unique features suited for specific applications, enhancing the efficiency of data representation and processing. Through mastering these conversions, one gains deeper insights into how data is managed within computer systems.
