next up previous
Next: A real computer Up: ho Previous: Instruction Execution on MU0

Subsections

Numbers and Characters

Numbers and Characters

Binary, Octal and Hexadecimal Numbers

Almost all computers store numbers as binary i.e. in base 2 rather than as decimal in base 10. Each number consists of a string of bits (binary digits), each of which can be either 0 or 1. e.g. using 16-bit strings, one is stored as the binary number 0000000000000001, two as 0000000000000010, three as 0000000000000011, four as 0000000000000100, ten as 0000000000001010, one hundred as 0000000001100100, one thousand as 0000001111101000, and ten thousand as 0010011100010000.
(We normally also use a special way of encoding negative numbers, known as 2s complement, that simplifies arithmetic using them. This will be described in CS1031.)

Binary numbers are not easy for humans to use, as we are so used to base 10. To try to make life a little easier, we usually group the bits together e.g. we could write ten thousand as 0 010 011 100 010 000 or 0010 0111 0001 0000. To make life even easier, we usually replace the groups of bits by the corresponding decimal value for each group, so 0 010 011 100 010 000 is usually written 023420 (octal notation) and 0010 0111 0001 0000 is usually written 2710 (hexadecimal notation, usually simply called hex).
What about the 4-bit groups with values 1010 to 1111 (ten to fifteen)? As there are no more decimal digits available, we start using the letters of the alphabet, replacing 1010 (ten) by A, 1011 (eleven) by B, 1100 (twelve) by C, 1101 (thirteen) by D, 1110 (fourteen) by E, and 1111 (fifteen) by F. (Alternatively, we can use a to f instead of A to F.)

So, the hexadecimal number 1A5E is equivalent to the binary number 0001 1010 0101 1110, which is the same as 0 001 101 001 011 110, which is equivalent to the octal number 015136. We can convert each of these to decimal:
0001101001011110 (binary, right-to-left) = 0*1 + 2 + 4 + 8 + 16 + 0*32 + 64 + 0*128 + 0*256 + 512 + 0*1024 + 2048 + 4096 + 0*8192 + 0*16384 + 0*32768 (decimal) = 6750
015136 (octal, right-to-left) = 6*1 + 3*8 + 1*64 + 5*512 + 1*4096 + 0*32768 = 6750
015136 (octal, left-to-right) = ((((0*8 + 1)*8 + 5)*8 + 1)*8 + 3)*8 + 6 = 6750
1A5E (hex, right-to-left, A=10, E=14) = 14*1 + 5*16 + 10*256 + 1*4096 = 6750
1A5E (hex, left-to-right, A=10, E=14) = (((1*16 + 10)*16) + 5)*16 + 14 = 6750

To convert a decimal number to binary (or octal or hex), simply divide it repeatedly by 2 (or 8 or 16), with the remainder at each stage being the next digit: 6750/2 = 3375 r 0 (so 0 is the least significant bit of the answer), 3375/2 = 1687 r 1 (so 1 is the next significant bit of the answer), 1687/2 = 843 r 1, 843/2 = 421 r 1, 421/2 = 210 r 1, 210/2 = 105 r 0, 105/2 = 52 r 1, 52/2 = 26 r 0, 26/2 = 13 r 0, 13/2 = 6 r 1, 6/2 = 3 r 0, 3/2 = 1 r 1, 1/2 = 0 r 1, and listing all the remainders gives us the equivalent binary value of 1 1010 0101 1110, and we can put extra 0s on the front to give us 0001 1010 0101 1110.

[Williams, sections 2.8 & 3.6]

Bits, Bytes, Words and Characters

A bit is a binary digit - that is, it is something whose value can be a 0 or a 1. If we collect several bits together, we can represent many more different values - 1 bit can represent 2 different values, 2 bits can represent 4 different values, 3 bits can represent 8 different values, and so on - n bits can represent $2^n$ different values.

We usually take these values to be different numbers (e.g. 0 to $2^n-1$, or $-2^{n-1}$ to $2^{n-1}-1$). However, as long as we agree on the meanings, any different values can be represented, such as letters of the alphabet or any other characters. A byte is 8 bits, and can be used to represent European characters. An early standard was ASCII, which only defines 128 different English and American characters. (An alternative encoding, now essentially abandoned, was EBCDIC.) This was extended to include 128 extra European characters by the ISO 8859 standard. (We sometimes refer to these as `7-bit ASCII' and `8-bit ASCII', respectively.) This in turn was extended to support characters sets used by the rest of the world in the Unicode standard - however this requires 16-bit and longer encodings. As an example of an 8-bit encoding, in all three standards the letter `A' is reresented by `0100 0001'. (For more information, use `man ascii' or `man iso_8859_1' or `man unicode' or `man utf-8' on a linux computer.)

A word can be any number of bits, but on most computers usually means the largest size that fits in registers etc.. This used to be 16-bits (like MU0), and nowadays is usually 32-bits. Because 16-bit computers were once so common, you may find 16-bits called a word and 32-bits a long-word, even on 32-bit computers.

Powers of 10 and of 2

Kilo 1K = 1000=$10^{3}$ = 1 thousand $ \approx 2^{10} $ = 1 024
Mega 1M = $ 10^{6} $ = 1 million $ \approx 2^{20} $ = 1 048 567
Giga 1G = $ 10^{9} $ = 1 US billion $ \approx 2^{30} $ = 1 073 741 824
Tera 1T = $ 10^{12} $ = 1 UK billion $ \approx 2^{40} $ = 1 099 511 627 776

In computing, powers of 10 are often approximated by powers of 2, so we talk about e.g. a Megabyte of memory meaning $2 ^ {20}$ bytes. However, we also talk about a MegaFLOPS meaning $10 ^ 6$ FLOPS. Originally, typical computer numbers were Ks, and the errors introduced were only about 2%. Nowadays we usually talk about Ms and Gs, and are starting to talk about Ts, where the differences are much bigger, so it is probably better to avoid the powers of 2 when there is any ambiguity.

milli: 1m = .001 = $10 ^ {-3}$ = 1 thousandth
micro: $ 1\mu = 10 ^ {-6}$ = 1 millionth
nano: 1n = $10 ^ {-9}$ = 1 US billionth

Speed

b/s, often written as bps, means bits per second.
B/s, often written as Bps, means bytes per second.
Hz, short for Hertz, means cycles per second.

If a computer transmits information down a cable in 32-bit words, sending another word every thousandth of a second, it is working at 1KHz, taking 1ms for each transmission, and sending a maximum of 32Kb/s or 4KB/s.


next up previous
Next: A real computer Up: ho Previous: Instruction Execution on MU0
Pete Jinks 2003-09-25