Skip to content
cyber journey

Resource Cheatsheet

Code: Alternative 10s

Code ch. 10 — counting in other bases: octal, base 4 and binary, converting between them, and where octal still shows up in Linux permissions.

Updated
Reading time
3 min read
Detail
Code (2nd ed.)
Detail
Ch. 10
On this page

In one line: ten is just one choice — count in eights, fours or twos and the same place-value rules apply, and binary is the one electricity loves.

Companion to: Charles Petzold, Code (2nd edition), chapter 10.


Place values by base

Base Digits Place values (right to left)
10 (decimal) 0–9 1, 10, 100, 1000
8 (octal) 0–7 1, 8, 64, 512
4 (quaternary) 0–3 1, 4, 16, 64
2 (binary) 0–1 1, 2, 4, 8, 16, 32, 64, 128
Decimal Octal Base 4 Binary
5 5 11 101
8 10 20 1000
10 12 22 1010
64 100 1000 1000000
100 144 1210 1100100
  • Base to decimal: multiply each digit by its place value, then add.
  • Decimal to base: divide by the base repeatedly and read the remainders from bottom to top.
  • One octal digit = three bits, so converting between octal and binary is just grouping.

Binary is the natural fit

Binary needs only two states — off/on, open/closed, 0/1 — exactly what switches, relays and transistors provide.

Circuits can decode a binary number into “which one of N lines is on” (a 3-to-8 decoder), or encode it back.


Octal in real life: Linux permissions

Octal Binary Permission
7 111 rwx
6 110 rw-
5 101 r-x
4 100 r--
0 000 ---
chmod 750 script.sh          # owner rwx, group r-x, others ---
stat -c '%a %A %n' script.sh # 750 -rwxr-x--- script.sh

Try it

oct(493)          # '0o755'
int('755', 8)     # 493
bin(100)          # '0b1100100'
int('1210', 4)    # 100
[Convert]::ToString(493, 8)    # 755
[Convert]::ToInt32('755', 8)   # 493

🔐 Security and IT connections

  • Reading permissions fast: 777 means anyone can write, which is almost always wrong. 600 is right for private keys.

  • A fourth leading digit adds special bits. 4755 is SUID: the program runs with its owner’s rights. Unusual SUID files are a classic privilege-escalation hunt:

    find / -perm -4000 -type f 2>/dev/null

Practice drills

1. Convert octal 17 to decimal.

1×8 + 7 = 15

2. What does chmod 640 give?

rw-r----- — owner read/write, group read, others nothing.

3. Convert binary 101 110 to octal.

Group into threes: 101 = 5, 110 = 6 → octal 56.


Key takeaways

  • Place value works in every base; only the multiplier changes.
  • Binary suits circuits because it needs just two states.
  • Octal packs three bits per digit — which is why Linux permissions are written as 755 and 640.

Search