# Understanding Number Systems

Number systems use different number bases. A number base indicates how many different digits are available when using a particular numbering system. For example, decimal is number base 10, which means it uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. Binary is number base 2, which means that is uses two digits: 0 and 1. Different number bases are needed for different purposes. Humans use number base 10 whereas computers use binary.

The number base determines how many digits are needed to represent a number. For example, the number 78 in decimal (base 10) requires two digits. The binary (base 2) equivalent is 1001110 which requires seven digits. As a consequence of this, there are many occasions in computing when very long binary numbers are needed. To solve this problem, other number bases can be used, which require fewer digits to represent numbers. For example, some aspects of computing use number base 16 which is referred to as hexadecimal.

The accepted method of representing different number bases (in technical documentation) is to show the number with the base in subscript. For example:

• 3710 is decimal
• 11012 is binary

You will often see hexadecimal numbers represented using the C programming syntax where the prefix 0x is added to the value: 0x12FF.

Hexadecimal (or Hex) is particularly useful for representing large numbers as fewer digits are required. Hex is used in a number of ways. Memory addresses are shown in hex format, as are colour codes. The main advantage of hex is that two digits represent one byte.

Consider the number 100010012. This is an 8-bit number which when converted to decimal equals 13710. The same number in hex is 8916. This basic example shows that an 8-bit number in binary can be represented as a two-digit number in hex.

As it is number base 16, hex uses 16 different digits: 0 to 9 and A to F. The image below shows a hex viewer displaying all byte values from 0 to 255 in decimal, or 0 to FF in hex. # Working with Number Bases

When performing calculations, humans use number base 10, probably because we have 10 digits on our hands. Commonly, this system is known as decimal and uses 10 different digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. When we get to 9 we add an extra digit to the left and start again. When we get to 99 we add an extra digit to the left and so on. Each digit we add is worth ten times the previous digit. The weighted value for each position is as follows: The number 3265 is easy to understand in decimal terms. It is made up as follows:

`(3 x 1000) + (2 x 100) + (6 x 10) + (5 x 1)`

When creating a number, we start with the units and add further digits as needed to create the number we want.

Binary numbers use number base 2 and works on exactly the same principal. This time, we only have two digits: 0 and 1. It has to be binary because computers work with either a zero or a one (off and on). Therefore, 1 is the largest value we can have with one bit. To increase the size of the number, we add more bits. Each bit is worth two times the previous bit because we are using number base 2. The table below shows an 8-bit binary number 10010111. The value of each new bit doubles in value as binary is base 2. Using the same principal to work out the number, we have:

`(1 x 128) + (1 x 16) + (1 x 4) + (1 x 2) + (1 x 1) = 151`

# Binary to Decimal Conversion

Binary numbers can be converted to decimal integers as follows:

• Write down a binary number (e.g. 10010111).
• Above the number, starting from the least significant bit (LSB) write the number 1.
• As you move left from the LSB to the most significant bit (MSB) double the value of the previous number. Wherever there is a 1, add the decimal value. The above example represents:

`(1 x 128) + (1 x 16) + (1 x 4) + (1 x 2) + (1 x 1) = 151`

Therefore, the binary value 1001011 equals 151 as a decimal integer.

# Decimal to Binary Conversion

To convert a decimal integer to a binary number, use the same method as above, but work in reverse. For example, to convert the number 151, write down the power of 2 sequence as follows: • Starting from the MSB, place a 1 or 0 in each column as necessary to ensure that it adds up to 151.
• 128 is less than 151, so place 1 in the first column: 151 – 128 = 23
• 64 is greater than 23, so place 0 in the second column
• 32 is greater than 23, so place 0 in the third column
• 16 is less than 23, so place 1 in the fourth column: 23 – 16 = 7
• 8 is greater than 7, so place 0 in the fifth column
• 4 is less than 7, so place 1 in the sixth column: 7 – 4 = 3
• 2 is less than 3, so place 1 in the seventh column: 3 – 2 = 1
• 1 equals 1, so place 1 in the eighth column. 1 – 1 = 0

Therefore 10010111 = 151

An alternative method of carrying out this calculation is to carry out repeated division on the decimal number as follows:

• 151 divided by 2 = 75 with a remainder of 1
• 75 divided by 2 = 37 with a remainder of 1
• 37 divided by 2 = 18 with a remainder of 1
• 18 divided by 2 = 9 with a remainder of 0
• 9 divided by 2 = 4 with a remainder of 1
• 4 divided by 2 = 2 with a remainder of 0
• 2 divided by 2 = 1 with a remainder of 0
• 1 divided by 2 = 0 with a remainder of 1

You keep dividing by 2 until there is nothing left to divide. Reading from the bottom up gives us 10010111 which equals 151. You can check the answer by working back the other way:

`128 + 16 + 4 + 2 + 1 = 151`

# Decimal to Hex Conversion

A common approach for converting decimal integers to hex is to first convert the decimal to binary and then convert the binary to hex. Taking the decimal number 211 as an example:

• Calculate the binary equivalent: • Split the binary number into two groups of four bits and convert each group into the hex equivalent. • 110100112 = 21110
• First group: 8 + 4 + 1 = 13 (or D in hex)
• Second group: 2 + 1 = 3

Therefore 110100112 = 21110 = D316 (0xD3).

# Hex to Decimal Conversion

The easiest way to convert hex to decimal is to first convert the hex to binary, and then binary into decimal. Hex to binary conversions are a reverse of the above process. Take the hex value, and then convert each digit in turn into its binary equivalent using groups of four bits (commonly referred to as a nibble). We will use 2A316 (0x02A3) as an example: `2 = 0010, A = 1010, 3 = 0011`

Therefore 10101000112 is the binary equivalent to 2A316.

This binary value can then be converted into decimal using the method outlined above. `512 + 128 + 32 + 2 + 1 = 67510`