Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford) Counting…. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, … What comes next? Why do we have ten digits? Number Representations We are used to base/radix 10 (decimal) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 10, 20, 30, …, 100 10, …, 100, …, 1000, …, 10 000 Decimal Decimal Example: 1041.20310 : 1 0 4 1 103 102 101 100 . 2 0 3 10-1 10-2 10-3 Represents: 1*1000 + 0*100 + 4*10 + 1*1 + 2/10 + 0/100 + 3/1000 Number Representations General radix r number, Nr dp dp-1 … d2 d1 d0 . rp rp-1 … r2 r1 r0 d–1 d–2 … d–q r-1 r-2 … r-q Represents: dprp + dp-1rp-1+ …+ d2 r2 + d1r1 + d0 r0 + d–1r--1+ d–2r--2+…d–qr-q Binary Computer uses presence/absence of voltage Digits 0 and 1(off/on). i.e. base 2 Bit = Binary digit Binary Binary Example: 1001.1012 : 1 0 0 1 23 22 21 20 . 1 0 1 2-1 2-2 2-3 Represents: 1*8 + 0*4 + 0*2 + 1*1 + 1/2 + 0/4 + 1/8 = 9.62510 Binary Integers (whole numbers) stored in n-bit words n-bit binary number has range: 0 to (2n-1) Largest 8-bit number: = 1111 11112 = 1 0000 00002 - 12 = 28 -1 = 25610 - 110 = 25510 Binary to Decimal Conversion 1. quot = number; i = 0; 2. repeat until quot = 0: 1. quot = quot/2; 2. digit i = remainder; 3. i++; gives digits from least to most significant bit Example: 33/2 = 16 16/2 = 8 8/2 = 4 4/2 = 2 2/2 = 1 1/2 = 0 rem 1 rem 0 rem 0 rem 0 rem 0 rem 1 => 3310 = 1000012 (lsb) (msb) Converting fractional numbers Convert int and frac. parts separately i = 0; repeat until N = 1.0 or i = n: N = FracPart(N); N *= 2; digit i = IntPart(N); i++ Example: 0.8125*2 = 1.625 0.625 *2 = 1.250 0.25 *2 = 0.500 0.5 *2 = 1.000 int = 1 int = 1 int = 0 int = 1 => 0.812510 = 0.11012 Caution: Many numbers cannot be represented accurately: 0.310 = 0.0[1001]...2 (bracket repeats, limited by bit size) (msb) (lsb) Binary Addition Adding binary numbers: 1+0=0+1=1 0+0=0 1 + 1 = 0 carry 1 Add 10910to 13610: 0110 1101 +1000 1000 ____________ Binary Addition Adding binary numbers: 1 + 0 = 0 + 1 = 1; 0 + 0 = 0; 1 + 1 = 0 carry 1 Add 10910to 13610: 0110 1101 +1000 1000 1111 0101 Check it! Binary Addition: Overflow Possibility of overflow Add 210 to 25410 : 1111 1110 + 0000 0010 [1] 0000 0000 = 25610 We only have 8 bits to store answer...so it's zero! Binary Addition: Overflow Overflow: what happens? Program can generate an “exception” to let us know Usually many bits used (e.g. Intel word is 32-bits) so occurrence is reduced Signed Numbers So far, only have positive numbers What about negative numbers? What about subtraction? Signed Numbers Can use left-most bit to encode sign 0 -> pos, 1 -> neg +510 0 00001012 -510 1 00001012 Signed Numbers For 8-bits, range is: -(27-1) 1 11111112 -12710 +(27-1) 0 11111112 +12710 Problems: – Two zeros!! – Addition not straight forward – Must check sign-bit before adding (bad for hardware implementors) This is non-sensical and wasteful: can use extra bit pattern One’s Complement Try “one's complement”: – negative numbers obtained by flipping bits – positive numbers unchanged – Example: +510 0000 01012 -510 1111 10102 Left-most bit still indicates sign Subtraction with One’s complement Now easy to subtract: complement number and add: 5-6 = 0000 0101 + complement (0000 0110) = 0000 0101 + 1111 1001 = 1111 1110 = complement (0000 0001) = (-1) Subtraction with One’s complement Caution: If overflow, – if numbers have different signs, carry is added into lsb – if numbers have same sign, actual overflow has occured Subtraction with One’s complement Evaluate 10–7 using 8-bit one’s complement arithmetic: Subtraction with One’s complement Evaluate 10–7 using 8-bit one’s complement arithmetic: 10 - 7 = 0000 1010 + complement (0000 0111) = 0000 1010 + 1111 1000 = 0000 0010 carry 1 = 0000 0010 + 0000 0001 = 00000011 = 310 Two’s Complement Still have two zeros two’s complement – Complement then add 1 – Our number range now asymmetric: -27 to 27-1 – Used extra zero bit pattern Two’s Complement Now when we add, discard carry 10 - 7 = 00001010 + 2complement (00000111) = 00001010 + 11111001 = 00000011 carry 1 (discard) = 3 Same overflow test can be used (same/different signs) Octal and Hexadecimal Base 8 (octal) and base 16 (Hexadecimal) are sometimes used (powers of 2) Octal uses 8 digits (0-7) Hex uses 16 “digits”: 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9, A, B, C, D, E, F Octal and Hexadecimal Each octal digit represents 3-bits Each hex digit represents 4-bits Examples: 1310 = 11012 = (001)(101)2 = (1101)2 = 158 = D16 Octal and Hexadecimal Conversion from decimal as for bin: – divide/multiply by 8 or 16 instead – May be easier to convert to binary first Binary to octal or hexadecimal – group bits into 3 (octal) or 4 (hex) from LS bit – pad with leading zeros if required Octal and Hexadecimal: Example 0100 0110 1101 01112 Octal and Hexadecimal: Example 0100 0110 1101 01112 = (000) (100) (011) (011) (010) (111) = 433278 = (0100) (0110) (1101) (0111) = 46D716 Note padding at front of number Octal and Hexadecimal To convert from hex/octal to binary: reverse procedure FF16 = (1111)(1111)2 3778 = (011)(111)(111)2 NOTE: for fractional conversion, move from left to right and pad at right end: 0.110011010112 0.112 = 0.(110)2 = 0. (110) (011) (010) (110) = 0.63268 = 0.68 Convert fractional/integer part separately