Numbering Systems (bases 2, 10, and 16)
What is a number?
While seemingly a deeply philosophical question, the way numbers are represented in electronics is different from the decimal system we naturally work with. Understanding and being able to transition between these numbering systems is a useful skill to have.
There are infinitely many ways to represent a number, but the four you will typically work with in software are decimal (base 10), binary (base 2), octal (base 8), and hexadecimal (base 16). Octals are rarely used today, as they come from a time when processors were 8-bit (but the theory is exactly the same)—you typically will work with one of the other three.
The key to understanding different numbering systems is the notion of place value. If you recall from elementary school, you probably learned that for decimals, the rightmost place denotes the value of the ones place, then the next corresponds to the tens place, then the hundreds... What this means is that for each digit, its value depends on its position. If you understand decimal (base 10) and place value, you should be able to understand any of the other numbering systems.
Decimal: Base 10
There are 10 unique digits in decimal (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) which represent an integer value 0-9. In general, what this means is that for a decimal number with digits say
abcdefgh
this is actually
Decimal Digits
Digit | Numerical Value |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
Examples
Here's an example (hopefully it's just a review)
Example
The number
9001
can be represented as
Counting in Decimal
Here's how to count from 0 to 32 in decimal.
Number | Decimal |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
10 | 10 |
11 | 11 |
12 | 12 |
13 | 13 |
14 | 14 |
15 | 15 |
16 | 16 |
17 | 17 |
18 | 18 |
19 | 19 |
20 | 20 |
21 | 21 |
22 | 22 |
23 | 23 |
24 | 24 |
25 | 25 |
26 | 26 |
27 | 27 |
28 | 28 |
29 | 29 |
30 | 30 |
31 | 31 |
32 | 32 |
Binary: Base 2
Binary is the natural way most digital circuits represent and manipulate numbers. There are 2 unique digits in binary (0, 1) which represent an integer value of 0 or 1. The numbering system works similarly to decimal, where the rightmost place represents the smallest unit (1), then the next corresponds to the next-smallest (2), then the one following (4), and so on... In general, what this means is that for a number (in binary) with digits say
pqrstuvw
this is actually
Binary Digits
Digit | Numerical Value |
---|---|
0 | 0 |
1 | 1 |
Examples
Here's a few examples
Example 1
The number
145
can be represented as
or
0b10010001
Example 2
The number
255
can be represented as
or
0b11111111
Counting in Binary
Counting in binary is similar to counting in decimal, except there are only 2 digits. Here's how to count from 0 to 32 in binary.
Number (decimal) | Binary |
---|---|
0 | 0b000000 |
1 | 0b000001 |
2 | 0b000010 |
3 | 0b000011 |
4 | 0b000100 |
5 | 0b000101 |
6 | 0b000110 |
7 | 0b000111 |
8 | 0b001000 |
9 | 0b001001 |
10 | 0b001010 |
11 | 0b001011 |
12 | 0b001100 |
13 | 0b001101 |
14 | 0b001110 |
15 | 0b001111 |
16 | 0b010000 |
17 | 0b010001 |
18 | 0b010010 |
19 | 0b010011 |
20 | 0b010100 |
21 | 0b010101 |
22 | 0b010110 |
23 | 0b010111 |
24 | 0b011000 |
25 | 0b011001 |
26 | 0b011010 |
27 | 0b011011 |
28 | 0b011100 |
29 | 0b011101 |
30 | 0b011110 |
31 | 0b011111 |
32 | 0b10000 |
Hexadecimal: Base 16
Hexadecimal—also known as hex or base 16—is a system we can use to represent numerical values in a way that is more compact than binary. Hex, like the decimal system, combines a set of 16 unique digits to create large numbers.
Hexadecimal Digits
Digit | Numerical Value |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 |
Examples
Here's a few examples
Example 1
The number
9
can be represented by
or
0x9
Example 2
The number
100
can be represented as
or
0x64
Example 3
The number
140
can be represented as
or
0x8C
Example 4
The number
255
can be represented as
or
0xFF
Counting in Hex
Counting in hex is similar to counting in decimal, except there are an additional 6 digits. Here's how to count from 0 to 32 in hex.
Number (decimal) | Hex |
---|---|
0 | 0x00 |
1 | 0x01 |
2 | 0x02 |
3 | 0x03 |
4 | 0x04 |
5 | 0x05 |
6 | 0x06 |
7 | 0x07 |
8 | 0x08 |
9 | 0x09 |
10 | 0x0A |
11 | 0x0B |
12 | 0x0C |
13 | 0x0D |
14 | 0x0E |
15 | 0x0F |
16 | 0x10 |
17 | 0x11 |
18 | 0x12 |
19 | 0x13 |
20 | 0x14 |
21 | 0x15 |
22 | 0x16 |
23 | 0x17 |
24 | 0x18 |
25 | 0x19 |
26 | 0x1A |
27 | 0x1B |
28 | 0x1C |
29 | 0x1D |
30 | 0x1E |
31 | 0x1F |
32 | 0x20 |
Protip
You can think of hex as just binary, but with groups of 4. So, when performing the conversion from binary to hex
- Pad with any necessary 0s so the number of digits is divisible by 4.
- Group the binary representation into groups of 4.
- Convert each group of 4 to the hex digit
Example
Consider the number
159
which has the binary representation
0b10011111
and can be grouped into two groups of 4 binary digits
1001 1111
We know that
0b1001 = 0x9
and
0b1111 = 0xF
so the hex representation is
0x9F
Base n
In general, for any numbering system with base b, we can represent a number with n + 1 digits as