# Octal

*Three bits to a digit*

Octal is base 8: each octal digit runs 0 to 7 and stands for exactly three binary bits, making it a compact shorthand for binary that predates hexadecimal and still appears in Unix file permissions and some legacy encodings.

Group: Number & logic
URL: https://digiwleea.wleeaf.dev/learn/octal/

[Hexadecimal](https://digiwleea.wleeaf.dev/learn/hexadecimal/) shrinks long [binary](https://digiwleea.wleeaf.dev/learn/binary/) strings by packing **four** bits into one digit. **Octal** does the same trick with **three** bits per digit, so it uses only the digits `0` through `7`. It is the older shorthand (it fit the 6-, 12-, and 36-bit machines of the 1960s, whose word sizes divided evenly by 3), and hex has mostly replaced it, but octal still turns up, most famously in Unix file permissions like `chmod 755`.

## Converting: group bits by three

Because one octal digit is exactly three bits, converting is pure grouping, no arithmetic. Starting from the **right**, split the binary into groups of three bits and write the value of each group:

```
110 101 = 6 5 = 65 (octal)      (110 = 6, 101 = 5)
```

Going the other way, expand each octal digit into its three bits: `72` in octal is `111 010` in binary. If the leftmost group is short, pad it with leading zeros. Compare this to hex, which groups by four: the same byte `0b11101010` is `EA` in hex (two groups of four) but `352` in octal (`11 101 010`, padded to `011 101 010`).

| octal digit | three bits | decimal |
| --- | --- | --- |
| 0 | 000 | 0 |
| 3 | 011 | 3 |
| 5 | 101 | 5 |
| 7 | 111 | 7 |

_Each octal digit is a fixed 3-bit pattern. There is no digit above 7, because three bits top out at 111 = 7._

> **WARN:** **Common mistakes.** Octal has no `8` or `9` digit, so `18` is not a valid octal number. Group bits from the **right**, not the left, or a trailing bit lands in the wrong digit. And watch the leading-zero convention: in many programming languages a number written with a leading `0` (like `0755`) is read as octal, a classic source of bugs when someone means the decimal `755`.

**Q (Try it):** Convert the binary `110101` to octal, then to decimal, and check they agree.

**A:** Group by three from the right: `110 101` = `6 5` = `65` octal. As decimal: `6 x 8 + 5 = 53`. Check directly from binary: `110101 = 32 + 16 + 4 + 1 = 53`. They agree, as they must, since octal is just a regrouping of the same bits.

### FAQ

**Q:** What is octal?

**A:** Octal is base 8: it uses the digits `0` to `7`, and each digit stands for exactly three binary bits. It is a compact shorthand for binary, older than [hexadecimal](https://digiwleea.wleeaf.dev/learn/hexadecimal/), still seen in Unix file permissions and some legacy machine encodings.

**Q:** How do you convert binary to octal?

**A:** Split the binary into groups of three bits starting from the right (pad the leftmost group with zeros if needed), then write the value of each group. For example `110101` becomes `110 101` which is `65` in octal.

**Q:** What is the difference between octal and hexadecimal?

**A:** Both are shorthands for binary, but octal packs **three** bits per digit (digits `0` to `7`) while [hexadecimal](https://digiwleea.wleeaf.dev/learn/hexadecimal/) packs **four** (digits `0` to `F`). Hex lines up with 8-, 16-, and 32-bit words, which is why it replaced octal on modern machines.

> **KEY:** Octal, hex, and decimal are all just [positional bases](https://digiwleea.wleeaf.dev/learn/number-bases/) over the same bits. Next, the [units of storage](https://digiwleea.wleeaf.dev/learn/units-of-storage/) lesson counts how many of those bits make a byte and a kilobyte.
