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

DigitNumerical Value
00
11
22
33
44
55
66
77
88
99

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.

NumberDecimal
00
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
3131
3232

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

DigitNumerical Value
00
11

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
00b000000
10b000001
20b000010
30b000011
40b000100
50b000101
60b000110
70b000111
80b001000
90b001001
100b001010
110b001011
120b001100
130b001101
140b001110
150b001111
160b010000
170b010001
180b010010
190b010011
200b010100
210b010101
220b010110
230b010111
240b011000
250b011001
260b011010
270b011011
280b011100
290b011101
300b011110
310b011111
320b10000

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

DigitNumerical Value
00
11
22
33
44
55
66
77
88
99
A10
B11
C12
D13
E14
F15

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
00x00
10x01
20x02
30x03
40x04
50x05
60x06
70x07
80x08
90x09
100x0A
110x0B
120x0C
130x0D
140x0E
150x0F
160x10
170x11
180x12
190x13
200x14
210x15
220x16
230x17
240x18
250x19
260x1A
270x1B
280x1C
290x1D
300x1E
310x1F
320x20

Protip

You can think of hex as just binary, but with groups of 4. So, when performing the conversion from binary to hex

  1. Pad with any necessary 0s so the number of digits is divisible by 4.
  2. Group the binary representation into groups of 4.
  3. 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