Logical Bases: Representation of Numbers
Total Page:16
File Type:pdf, Size:1020Kb
Logical Bases: Representation of Numbers Decimal numbers most commonly used by humans are in base 10 and consist of the ten decimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. For example, the decimal number 373 is 373 = 3*100 + 7*10 + 3 Positional notation describes the numbers according to position. For example, the two 3s in the decimal number 373 are in different positions, the 3 in the first position (at left) has a value which is 100 times more than the other 3. This notation can be described by a data flow diagram as follows at the left below. Binary numbers are most commonly used in digital computing; they have base 2. The data flow diagram at the right shows the binary number 1101 which is the decimal number 13. 1101 = 1*8 + 1*4 + 0*2 + 1*1 = 13 in decimal Decimal numbers (base 10) Binary numbers (base 2) 3 7 3 1 1 0 1 10 2 100 10 1 8 4 2 1 * * * * * * * 300 70 3 8 4 0 1 + + 373 13 1 Binary Addition To add the ten decimal digits requires a table of 10*10 = 100 entries; but if the digits are binary a table of only 2*2 = 4 rows is needed. The binary add definition follows: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 2 = 10 = 0 with carry of 1 A table of such a simple adder (half adder) follows, showing the output of a sum s and a carry c. A logical gate diagram shows that the carry is simply the And of a, b. The sum is actually the exclusive-Or, which is formed in detail below. HA = Half Adder in out a b s c a | b & s 0 0 0 0 a ! 0 1 1 0 b 1 0 1 0 a & c 1 1 0 1 b carry 2 Longer Binary Addition Addition of two numbers, say A=3=11 and B=6=110 to yield a third D is done in stages. At each stage there is input a bit value a, and b, and a carry from the previous stage. Then there is an output of a sum s, and a carry to the next stage. This process begins with the least significant digits (at the right) and proceeds to the left. The previous Half Adders can be used as shown to create a Full Adder. A = 3 = 0 0 1 1 FA = Full Adder c a A HA B = 6 = 0 1 1 0 s b B c 0 1 1 0 Carry FA FA FA FA 0 c a 1 HA s b c D = 9 = 1 0 0 1 sum 3 General Numbers: to any base b In general, a number can be represented in any base b as shown. The base may be any integer; the bases 2, 8, 10, 16 are most common. However, there may be unusual bases, negative values, fractions, etc. The coefficients c0, c1, c2, .. are usually integers from some small set, usually 0, 1, 2, etc up to the value of the base. General numbers (base b) cn ... c2 c1 c0 b bn b2 b1 b0 * * * * + cn*bn + .. + c2*b2 + c1*b1 + c0*b0 4 Numbers in base 3 Numbers in base 3 have the 3 coefficients, 0, 1 and 2. Some numbers, from 0 to 12, are shown in this base Note the beauty of this table; group them 3 at a time. Ternary numbers (base 3) i base3 2 0 1 3 0 000 1 001 27 9 3 1 2 002 3 010 * * * * 4 011 5 012 6 020 + 7 021 8 022 9 100 Example: 10 101 2 1 0 11 102 (201)3 = 2*3 + 0*3 + 1*3 12 110 = 2*9 + 0*3 + 1*1 = 18 + 0 + 1 = 19 (201)3 = (19)10 5 Numbers in base 8 (octal) Numbers in base 8 have the 8 coefficients, 0, 1, 2, .. 7. Some numbers, from 0 to 12 are shown in this base 8. Octal numbers (base 8) i octal cn ... c2 c1 c0 8 0 0 1 1 512 64 8 1 2 2 3 3 * * * * 4 4 5 5 6 6 + 7 7 8 10 9 11 cn*8n + .. + c2*82 + c1*81 + c0*80 10 12 Example: 11 13 (201) = 2*82 + 0*81 + 1*80 8 12 14 = 2*64 + 0*8 + 1*1 = 128 + 0 + 1 = 129 (201)8 = (129)10 6 Numbers in base 16 (Hexadecimal) Numbers in base 16 have the 16 coefficients, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Some numbers, from 0 to 16 are shown in this base This hexadecimal base (called Hex) is very common in computing. Octal numbers (base 8) i hex cn ... c2 c1 c0 0 0 16 1 1 4096 256 16 1 2 2 3 3 4 4 * * * * 5 5 6 6 + 7 7 8 8 9 9 cn*16n + .. + c2*162 + c1*161 + c0*160 10 A Example: 11 B 2 1 0 (201)16 = 2*16 + 0*16 + 1*16 12 C = 2*256 + 0*16 + 1*1 13 D = 512 + 0 + 1 14 E = 513 15 F (201)16 = (513)10 16 10 7 Numbers in base -2 Bases can be negative;the following one is binary base (-2). The coefficients are still binary, 0s and 1s only. Some numbers, both positive and negative are shown in this base This is not a common way to create negative numbers. Binary numbers (base -2) cn ... c2 c1 c0 i base -2 i base -2 -2 0 0 0 0 1 1 -1 11 -8 4 -2 1 2 110 -2 10 3 111 -3 1101 * * * * 4 100 -4 1100 5 101 -5 1111 + 6 11010 -6 1110 7 11011 -7 1001 8 110000 -8 1000 cn*bn + .. + c2*b2 + c1*b1 + c0*b0 8 Numbers in base 2 (binary) with negative coefficients Bases can be positive with negative coefficients; The following one has a binary base 2 with coefficients 1, 0 and -1. The coefficient -1 can be written as 7, or as a 1 with a bar over it. Some numbers, both positive and negative are shown in this base This is also not a common way to create negative numbers. Binary numbers (base 2) i base 2 i base 2 cn ... c2 c1 c0 0 0 0 0 2 1 1 -1 7 8 4 2 1 2 10 -2 70 3 11 -3 77 4 100 -4 700 * * * * 5 101 -5 707 6 110 -6 770 + 7 111 -7 777 8 1000 -8 7000 cn*bn + .. + c2*b2 + c1*b1 + c0*b0 9 Common Binary Procrustean representation Most computers are binary with a fixed number of bits (8, 16, 24, 32, 64). They are called procrustean, after the Greek legend of Procrustes, or PolyDemon, who had an iron bed, placed people on it, and if they were too tall he chopped them off to the fixed size; if they were too short he stretched them to this fixed size. Computer registers, words, etc are also fixed size. Signed Magnitude (s&m) A simple way of representing binary numbers which are positive or negative is to specify the sign as the first binary digit (or bit). A negative sign is 1; a positive sign is 0. For example, with 4 bits, there are 16 possible integers as shown. Notice however that there are two zeros, a positive and a negative one. Not good! Binary numbers (base 2) i s&m i s&m +/- c2 c1 c0 0 0000 -0 1000 2 1 0001 -1 1001 4 2 1 2 0010 -2 1010 Sign 3 0011 -3 1011 4 0100 -4 1100 0 is + * * * 1 is - 5 0101 -5 1101 6 0110 -6 1110 + 7 0111 -7 1111 10 Ones Complement Another way of representing negative numbers is to do a complement; to complement or “Not” each bit of the positive number to get the negative. For example, with 4 bits, the 16 possible integers are shown below. Notice again however that there are two zeros, a positive and a negative one. Not good. The conversion of a number to its negative is shown in the data diagram below. i 1comp i 1comp cn ... c2 c1 c0 0 0000 -0 1111 2 1 0001 -1 1110 2 0010 -2 1101 ! ! ! ! 3 0011 -3 1100 4 0100 -4 1011 5 0101 -5 1010 cn c2 c1 c0 6 0110 -6 1001 2 7 0111 -7 1000 11 Twos Complement Yet another way of representing negative numbers is to do another kind of complement; to view the negative numbers as a cyclic odometer as on a vehicle. A 4 bit system, goes forward, from 0000 to 1111 and then repeats starting with 0000. Going backwards from 0000, the next value (which is 1111) corresponds to -1. Similarly for -2, -3, etc to -8. There is only one value for zero, and the values range from -8 to +7. In general for n digits the values range from -2n to 2n - 1 Such a 2s complement is most commonly used in computers, with more than 4 bits. The 2s complement negative numbers are just one more than the 2s complement, so to create the 2s complement, simply invert all the bits and add 1 to the result. i 2comp i 2comp +2 = 0010 0 0000 -1 1111 +2 = 0010 1 0001 -2 1110 +4 = 0100 2 0010 -3 1101 3 0011 -4 1100 -5 = 1011 4 0100 -5 1011 +2 = 0010 5 0101 -6 1010 -3 = 1101 6 0110 -7 1001 -5 = 1011 7 0111 -8 1000 -2 = 1110 -7 = 1001 The sum of two numbers in 2s complement is shown to the right, in three examples.