Part 32 Binary and hexadecimal Subjects covered... Number systems Bits and bytes This section describes how computers count, using the binary system. Most European languages count using a more or less regular pattern of tens - in English, for example, although it starts off a bit erratically, if soon settles down into regular groups... twenty, twenty one, twenty two... twenty nine thirty, thirty one, thirty two... thirty nine forty, forty one, forty two... forty nine ...and so on, and this is made even more systematic with the numerals that we use. However, the only reason for using ten (the decimal system) is that we happen to have ten digits on our hands (fingers and thumbs). Instead of using the decimal system - based on ten, computers use a form of binary called hexadecimal (or 'hex' for short) which is based on sixteen. As there are only ten digits available in our number system we need six extra digits to do the counting. So we use A, B, C, D, E and F. And what comes after F? Well, just as we, with ten fingers, write 10 for ten (a hand full), so computers use 10 for sixteen. Comparing counting in decimal to hex... DECIMAL HEX ------- --- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 A 11 B 12 C 13 D 14 E 15 F 16 10 17 11 ...continued... 25 19 26 1A 27 1B ...etc... 31 1F 32 20 33 21 ...etc... 158 9E 159 9F 160 A0 161 A1 ...etc... 255 FF 256 100 ...and so on... If you are using hex notation and you want to make the fact quite plain, then write 'h' at the end of the number, and say 'hex'. For instance, for one hundred and fifty eight (decimal), write '9Eh' and say 'nine E hex'. You may be wondering what all this has to do with computers. In fact, computers behave as though they had only two digits, represented by a low voltage (or off) known as 0, and a high voltage (or on) known as 1. This is called the binary system, and the two binary digits are called bits - so a bit is either 0 or 1. So to expand the previous table of counting to include binary... DECIMAL HEX BINARY ------- --- ------ 0 0 0 1 1 1 2 2 10 3 3 11 4 4 100 5 5 101 6 6 110 7 7 111 8 8 1000 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111 16 10 10000 17 11 10001 ...etc... It is customary to 'pad out' binary numbers with leading zeroes so that they always contain at least four bits - for example, 0000, 0001, 0010, 0011 (representing 0 to 3 decimal). Converting between binary and hex is very easy (use the previous table to help you). To convert a binary number to hex, split the binary number into groups of four bits (starting at the right of the number) and convert each group of four bits into is corresponding hex digit. Finally, put the hex digits together to form the complete hex number. For example, to convert 10110100 binary into hex, convert the first (right-hand) group of four bits (0100) to 4 hex, then convert the next group of four bits (1011) to 8 hex, put them together, and you have the complete hex number - B4h. If the binary number is longer than eight bits, you can continue converting each group of four into one hex digit. For example, 1101011110000 binary corresponds to 3AF0h. To convert a hex number to binary, change each hex digit into four bits (again, starting at the right) then put the bits together to form the complete binary number. For example, to convert F3h to binary, first convert 3 which corresponds to 0011 binary (remember - you must use zeroes to make the binary number four bits long), then convert F which corresponds to 1111 binary, put them together, and you have the complete binary number - 11110011. Although computers use a pure binary system, humans often write the numbers stored inside a computer using hex notations - after all, the number 3AF0h (for example) is far more likely to be easily and correctly read than 0011101011110000 in sixteen bit binary notation. The bits inside the computer are mostly grouped into sets of eight - these are called bytes. A single byte can represent any number from 0 to 255 decimal (11111111 binary or FFh). Two bytes can be grouped together to make what is technically called a word. A word can be expressed using sixteen bits or four hex digits, and represents a number from 0 to 65535 decimal (1111111111111111 binary or FFFFh). A byte is always eight bits, but words vary in length from computer to computer. The BIN notation (used in part 14 of this chapter) provides a means of entering numbers in binary on the +3, i.e. 'BIN 100' represents 4 decimal, 'BIN 111' represents 7 decimal, 'BIN 11111111' represents 255 decimal, and so on. You can only use 0s and 1s for this, so the number must be a non-negative whole number - for instance, you cannot use 'BIN -11' to represent -3 decimal, but you can use '-BIN 11' instead. The number must also be no greater than decimal 65535 - i.e. it can't have more than sixteen bits. If you pad out a binary number with leading zeroes, for example, 'BIN 00000001', the BIN function will rightly ignore them and treat the number as if it were 'BIN 1'.