Appendix: Decimal, Hexadecimal and Binary Number Systems
Total Page:16
File Type:pdf, Size:1020Kb
Appendix: Decimal, Hexadecimal and Binary Number Systems The decimal number system uses the ten digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 and is said to be a base 10 system. In any decimal number a digit is given a certain value that depends on its position in the given field of digits. The number 325, for example, is understood as 5 units plus 2 tens plus 3 hundreds and it can be interpreted as 5 x 10°, plus 2 x 101 , plus 3 x 102 or simply additions of multiple powers of 10. Any decimal number, including fractional decimal numbers, can be expressed this way. The hexadecimal number system uses sixteen digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F and is a base 16 system. As with the decimal number system, a digit has a value that is dependent on its position in a given field of digits. The first position in a hexadecimal number equates to the digit in that position multiplied by 16°. The second position equates to the digit times 161 and so on. The hexadecimal number 243 (written $243) may be interpreted as 3 x 16°,plus4 x 161 ,plus 2 x 162 which is 579. On this basis it is a simple matter to build up a table of figures for hexa decimal to decimal conversion, as shown in Table A. I. The second line of the table is 163 , 162 , 161 and tt;O, with all the other entries being multiples of that line. To use the table, the-entry for each hexadecimal digit and its corresponding position is simply added, since the multiplication has already been carried out. Example A.l. Hexadecimal to decimal conversion Given $3A4B $3000 is 12288 $ AOO is 2560 $ 40 is 64 $ B is 11 Hence $3A4B is 14923 249 250 MICROCOMPUTER APPLICATIONS IN STRUCTURAL ENGINEERING Table A.1 Hexadecimal-decimal conversion Hexadecimal Digit Position Digit 4th 3rd 2nd 1st 0 0 0 0 0 1 4096 256 16 1 2 8192 512 32 2 3 12288 768 48 3 4 16384 1024 64 4 5 20480 1280 80 5 6 24576 1536 96 6 7 28672 1792 112 7 8 32768 2048 128 8 9 36864 2304 144 9 A 40960 2560 160 10 B 45056 2816 176 11 c 49152 3072 192 12 D 53248 3328 208 13 E 57344 3584 224 14 F 61440 3840 240 15 For decimal to hexadecimal conversion, select the nearest number in the table to that given. This provides a hexadecimal digit and its position; repeat with the remainder. Example A.2. Decimal to hexadecimal conversion Given 5625 4096 is $1000 remainder 1529 1280 is $ 500 remainder 249 240 is $ FO remainder 9 9 is $ 9 remainder 0 Hence 5625 is $15F9 The binary number system uses only the two digits 0 and 1 and is a base 2 system. Again the digit has a value that is dependent on its position, this time equating to the digit multiplied by powers of 2, so that Table A.2 can be built up. The corresponding decimal-hexadecimal-binary relationship of Table A.3 follows immediately. APPENDIX: DECIMAL, HEXADECIMAL AND BINARY NUMBER SYSTEMS 251 Table A.2 Significance of binary digit position Decimal Value Binary Digit Position Power of2 Decimal 1st 20 1 2nd 21 2 3rd 22 4 4th 23 8 5th 24 16 6th 25 32 7th 26 64 8th 2' 128 Table A.3 Decimal-hexadecimal-binary Decimal Hexadecimal Binary 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 10 A 1010 11 B 1011 12 c 1100 13 D 1101 14 E 1110 15 F 1111 Conversion of decimal to binary and binary to decimal is conveniently carried out using hexadecimal code as an intermediate step. It should be noted that each digit in the fust position of a hexadecimal number can be represented by a 4-bit binary number. Each digit in the second position of a hexadecimal number can also be represented by a 4-bit binary number followed by 4 zero bits, while for a third position hexadecimal digit a 4-bit binary number followed by 8 zero bits is required. 252 MICROCOMPUTER APPLICATIONS IN STRUCTURAL ENGINEERING Example A.3. Hexadecimal to binary conversion Given $F3A9 $0009 is 1001 $00AO is 1010 0000 $0300 is 0011 0000 0000 $FOOO is 1111 0000 0000 0000 Hence $F3A9 is 1111 0011 1010 1001 In each case the bit pattern has come from Table A.3 using the hexadecimal digit without regard to its position. Conversion from binary to hexadecimal simply means taking the binary number in 4-bit patterns and assigning the cor responding hexadecimal digit to that position. Example A.4. Binary to hexadecimal conversion Given 1010 1100 0101 1001 1001 is $0009 0101 0000 is $0050 1100 0000 0000 is $0COO 1010 0000 0000 0000 is $AOOO Hence 1010 1100 0101 1001 is $AC59 Other number systems can be introduced on a similar basis and the octal number system using eight digits 0, 1, 2, 3, 4, 5, 6 and 7 is a base 8 system that is often used in digital computing. It should also be noted that fractional decimal numbers can be represented in other number systems. In the decimal system digits to the right of a decimal point are multiples of 10 raised to a negative power. The number 0.325 is (3 X 10-1) + (2 X 10-2) + (5 X 10-3) In a similar way in the binary system, positions to the right of a "decimal" point can be interpreted. The binary number 0.1000 is 1 x T 1 in decimal, which is 0.5. Some other fractional decimal to binary number conversions are given in Table A.4. Fractional decimal numbers are frequently expressed in exponent form and are referred to as floating point numbers. Indeed any non-integer number is usually described as a floating point number in the context of computers. The exponent indicates the number of positions that a decimal point has to be moved to give the number and this is given in the scientific notation represen tation of a decimal number. Any decimal number, apart from zero, can be written in the form ±Ax 10", where A is in the range 0.1 <;A< 1.0 APPENDIX: DECIMAL, HEXADECIMAL AND BINARY NUMBER SYSTEMS 253 Table A.4 Some fractional decimal to binary conversion Decimal Binary 0.5 (2-1) 0.1000 0.25 (T2) 0.0100 0.125 (T3) 0.0010 0.0625 (T4) 0.0001 Table A.S shows some decimal numbers in this form and in scientific notation. Table A.S Floating point number representation Number ±A X 10n Scientific Notation 5625.836 0.5625836 X 104 0.5625836E + 4 0.006258 o.6258ooo x 1 o-2 0.6258000E- 2 -15.23784 -0.1523784 X 102 -0.1523784E + 2 Such a method of representation need not be restricted to the decimal system. Any binary number, apart from zero, can be written in the form ±Ax 28 , where A is in the range of binary numbers O.I..;;A < 1 (which is the range of 1/2 o;;;;A < 1 in the decimal system). A is known as the mantissa and it should be noted that it is necessary to store only the sign, the mantissa and the exponent B to give a computer all the details of a floating point number. With an 8-bit microcomputer which uses a single byte to store the exponent, the range of B is from -127 to + 127 with B stored as B + 128; this ensures that B is always positive. The limit to the exponent arises since 127 + 128 is 255, which is the largest number that can be stored in 8 bits. The value B equal to 128 is reserved for the number zero. Table A.6 shows a series of binary numbers expressed in such a form. Only the bits of the mantissa to the right of the decimal point need be stored, the position of the decimal point being implied. Further, the most significant bit is always 1, so that this bit position can be used to indicate the sign of the number. For example, for a positive number this bit position is usually set to zero. 254 MICROCOMPUTER APPLICATIONS IN STRUCTURAL ENGINEERING Table A.6 Binary numbers in exponent form Number ±A x28 A B+ 12S 0001 0.1 X 21 0.1 129 0101 0.101 X 23 0.101 131 1000.1 0.10001 X 24 0.10001 132 1110 0.1110 X 24 0.1110 132 The previous discussion suggests a way in which a floating point number can be held in memory in a microcomputer. This can be done using 5 bytes, with the first byte holding the exponent in excess 12S mode and the remaining 4 bytes holding the mantissa. It is usually possible, by means of a suitable monitor on the microcomputer, to display the contents of memory locations byte by byte with the information returned in hexadecimal code. Alternatively a BASIC statement using the PEEK function can print out the byte contents in decimal code. For example, a monitor display of 5 bytes starting at a given memory address known to be the location of a floating point number could show S3 23 F3 5B AS which would be in hexadecimal code.