miSim DE Back Index Next

Appendix B

Number Formats

When counting, we normally use the Arabic number system and count in decimal (base ten) - using the digits '0' to '9'. However, computers are built around the binary number system (base two) where there are just two digits ('0' and '1').

In decimal, the right most digit represents 'units' - the numbers 0 to 9. The second digit from right represents 'tens' - it is multiplied by 10. The third digit represents 'hundreds' - it is multiplied by 100. In general, each digit is multiplied by 10 to the power of N (written as 10^N, meaning 10*10*10.. N times)- where N is the column that the digit is in, counting from column zero on the right.

DecimalColumn
3210
1234 =   1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0
1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1
1000 + 200 + 30 + 4

Binary suits computers because they can store the digits '0' and '1' as a circuit being 'off' or 'on'. The columns in a binary number are multiplied by different values - each column being multiplied by 2 to the power of N. Therefore the zero'th column is 2^0 (1), the first is 2^1 (2), the second is 2^2 (2*2 = 4), the third is (2*2*2 = 8) and so on.

BinaryColumn - 1
765 432 10
10110111 =    1 * 2^7 + 0 * 2^6 + 1 * 2^5 + 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0
1 * 128 + 0 * 64 + 1 * 32 + 1 * 16 + 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1
128 +   32 + 16 +   4 + 2 + 1
183 (Decimal)

Counting in binary suits computers, but it doesn't suit us. Simple microcontrollers store data as 8-bit (eight binary digits) values - which allows them to count from 0 to 255. Writing 255 in binary is: '11111111'. The same microcontrollers may use 14-bit addresses '11111111111111' - a bit hard to keep track of. Converting binary to decimal helps, but the conversion is not very convenient, because decimal digits don't correspond to an easy number of bits.

Hexadecimal (base sixteen) solves those problems. A single digit can have sixteen different values - which conveniently maps to exactly four bits. So our eight-bit values stored in our processor can be written as two digit hexadecimal numbers. Of course, we don't actually have sixteen digits to write with, so we have to make up some more. Counting in hexadecimal goes '0, 1, 2...' and up to '9', then we use 'A' (=10) as the eleventh digit, 'B' (=11) as the twelfth, up to 'F' (=15) as the sixteenth.

The columns have the value of 16^N, so we can read a hex number as follows:

HexadecimalColumn
210
1AF =   1 * 16^2 + A * 16^1 + F * 16^0
1 * 256 + A (=10) * 16 + F (=15) * 1
256 + 160 + 15
431 (decimal)

And the awkward value of '11111111' (binary) is quickly written as 'FF' (hex).

Index Next