DEV Community

Mahi
Mahi

Posted on

Numeral systems explained (binary, octal, hexadecimal...)

The most basic way of counting happens in an unary numeral system, where you add a new symbol for every new item. You're already familiar with it, it's what we teach children:

🍎 apple
🍎🍎 apples
🍎🍎🍎 apples
Enter fullscreen mode Exit fullscreen mode

However, writing large numbers in this manner would be more than troublesome, so us humans have invented alternative numeral systems where one symbol can be used to represent a larger quantity of items.

The most common of these systems is the decimal numeral system in base-10, where digits 0 through 9 represent a particular quantity:

0 =
1 = 🍎
2 = 🍎🍎
...
9 = 🍎🍎🍎🍎🍎🍎🍎🍎🍎
Enter fullscreen mode Exit fullscreen mode

The method for counting upwards in our decimal system works in the following way:

  1. increment the rightmost digit by one until it reaches its maximum value of 9,
  2. at which point reset it to its lowest value of 0 and increment the digit to its left by one.
  1
  2
  3
...
  8
  9 // (1) Rightmost digit reached its maximum value of 9
 10 // (2) Reset it to 0 and increment the digit to its left
 11
 12
...
 98
 99 // (1) Both rightmost and the one next to it reached maximum
100 // (2) Reset to 0 and increment the digit to their left
101
Enter fullscreen mode Exit fullscreen mode

This is how the unary system works as well, except the maximum, the minimum, and the only available digit is 1.

dec   unary
  0 = 
  1 =    1 // (1) Rightmost digit reached its maximum value of 1
  2 =   11 // (2) Reset it to 1 and increment the digit to its left
  3 =  111
  4 = 1111
Enter fullscreen mode Exit fullscreen mode

And yes, there is no zero in unaryβ€”you don't have "0 🍎", you just don't have anything at all.

Binary and Hexadecimal

Counting in the binary (base-2) numeral system works exactly the same way, except any one digit has two possible values, typically represented by 0 and 1:

dec   binary
  0 =   0
  1 =   1 // (1) Rightmost digit reached its maximum value of 1
  2 =  10 // (2) Reset it to 0 and increment the digit to its left
  3 =  11 // (1) Both rightmost and the one next to it reached maximum
  4 = 100 // (2) Reset to 0 and increment the digit to their left
  5 = 101
  6 = 110
  7 = 111
Enter fullscreen mode Exit fullscreen mode

Now with hexadecimal system being base-16β€”and therefore supporting more values per digit than our decimal systemβ€”we need new symbols to represent the values 10 through 15. Most commonly the letters A through F are used respectively:

dec   hexa
  0 =   0
  1 =   1
  2 =   2
   ...
  9 =   9
 10 =   A
 11 =   B
 12 =   C
 13 =   D
 14 =   E
 15 =   F // (1) Rightmost digit reached its maximum value of F
 16 =  10 // (2) Reset it to 0 and increment the digit to its left
 17 =  11
   ...
 30 =  1E
 31 =  1F // (1) Rightmost digit reached its maximum value of F
 32 =  20 // (2) Reset it to 0 and increment the digit to its left
 33 =  21
   ...
254 =  FE
255 =  FF // (1) Both rightmost and the one next to it reached maximum
256 = 100 // (2) Reset to 0 and increment the digit to their left
Enter fullscreen mode Exit fullscreen mode

As you can see, counting in any numeral system works exactly the same way, only the number of different quantities representable by any one symbol changes. This is basically what "base-n" means, with n being the number of options representable by one digit.

Repetition is the mother of learning

Notice that these numeral systems are nothing more but a visual representation of the underlying value, in other words the number of apples stays exactly the same:

Counting in different numeric systems

There are other commonly used numeral systems as well, such as:

  • base-60 used between hours and minutes or minutes and seconds in a clock,
  • base-12 used for months in a year or hours in the AM/PM system, or
  • base-7 used for days in a week.

Why so many systems?

Arguably the real world is closest represented by the unary numeral system, where having three apples is just having 🍎🍎🍎 and nothing more. But us humans typically find the decimal system easier to work with, simply due to how repetitive and troublesome it would be to write in unary.

However, for most computers the binary system is the only available option, as the transistors inside their electronic parts can only represent two states: ON or OFF, 1 or 0. This is why us programmers often need to understand various numeral systems well.

Systems like octal (base-8) and hexadecimal (base-16) are mostly used by humans in the context of programming and computer data, where writing out the individual bits of a number would be too troublesome:

hex  binary
 0 = 0000
 1 = 0001
 2 = 0010
 3 = 0011
 4 = 0100
 5 = 0101
 6 = 0110
 7 = 0111 // Octal system's digits 0-7 can represent up to three bits
 8 = 1000 // This would be 10 in octal, requiring two digits
 9 = 1001
 A = 1010
 B = 1011
 C = 1100
 D = 1101
 E = 1110
 F = 1111 // While one hexadecimal digit can represent up to four bits
Enter fullscreen mode Exit fullscreen mode

This way we can just write F0 0F when we mean 1111 0000 000 1111, for example.

Finally, especially in the context of programming where we can mix'n'match different numeral systems together in the same source file, we often use prefixes like 0b for binary and 0x for hexadecimal:

2 == 0b10 == 0x2
10 == 0b1010 == 0xA
32 == 0b10_0000 == 0x20
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Every base is base 10 πŸ˜‰

Collapse
 
mahime profile image
Mahi

Unary isn't πŸ˜†