Presentation is loading. Please wait.

Presentation is loading. Please wait.

Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)

Similar presentations


Presentation on theme: "Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)"— Presentation transcript:

1 Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)

2 Counting…. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, … What comes next? Why do we have ten digits?

3 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

4 Decimal Example: 1041.203 10 : Represents: Decimal 1*1000 + 0*100 + 4*10 + 1*1 + 2/10 + 0/100 + 3/1000 1 0 4 1. 2 0 3 10 3 10 2 10 1 10 0 10 -1 10 -2 10 -3

5 Number Representations General radix r number, N r Represents: d p d p-1 … d 2 d 1 d 0. d –1 d –2 … d –q r p r p-1 … r 2 r 1 r 0 r -1 r -2 … r -q d p r p + d p-1 r p-1 + …+ d 2 r 2 + d 1 r 1 + d 0 r 0 + d –1 r --1 + d –2 r --2 +…d –q r -q

6 Binary Computer uses presence/absence of voltage Digits 0 and 1(off/on). i.e. base 2 Bit = B inary dig it

7 Binary Binary Example: 1001.101 2 : Represents: 1*8 + 0*4 + 0*2 + 1*1 + 1/2 + 0/4 + 1/8 = 9.625 10 1 0 0 1. 1 0 1 2 3 2 2 2 1 2 0 2 -1 2 -2 2 -3

8 Binary Integers (whole numbers) stored in n-bit words n-bit binary number has range: 0 to (2 n -1) Largest 8-bit number: = 1111 1111 2 = 1 0000 0000 2 - 1 2 = 2 8 - 1 = 256 10 - 1 10 =255 10

9 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 rem 1(lsb) 16/2 = 8 rem 0 8/2 = 4 rem 0 4/2 = 2 rem 0 2/2 = 1 rem 0 1/2 = 0 rem 1(msb) => 33 10 = 100001 2

10 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 int = 1 (msb) 0.625 *2 = 1.250 int = 1 0.25 *2 = 0.500 int = 0 0.5 *2 = 1.000 int = 1 (lsb) => 0.8125 10 = 0.1101 2 Caution: Many numbers cannot be represented accurately: 0.3 10 = 0.0[1001]... 2 (bracket repeats, limited by bit size)

11 Binary Addition Adding binary numbers: 1 + 0 = 0 + 1 = 1 0 + 0 = 0 1 + 1 = 0 carry 1 Add 109 10 to 136 10 : 0 1 1 0 1 1 0 1 + 1 0 0 0 1 0 0 0 ____________

12 Binary Addition Adding binary numbers: 1 + 0 = 0 + 1 = 1; 0 + 0 = 0; 1 + 1 = 0 carry 1 Add 109 10 to 136 10 : 0 1 1 0 1 1 0 1 + 1 0 0 0 1 0 0 0 1 1 1 1 0 1 0 1 Check it!

13 Binary Addition: Overflow Possibility of overflow Add 2 10 to 254 10 : 1111 1110 + 0000 0010 [1] 0000 0000 = 256 10 We only have 8 bits to store answer...so it's zero!

14 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

15 Signed Numbers So far, only have positive numbers What about negative numbers? What about subtraction?

16 Signed Numbers Can use left-most bit to encode sign 0 -> pos, 1 -> neg +5 10 0 0000101 2 -5 10 1 0000101 2

17 Signed Numbers For 8-bits, range is: 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 -(2 7 -1) +(2 7 -1) 1 1111111 2 0 1111111 2 -127 10 +127 10

18 One’s Complement Try “one's complement”: –negative numbers obtained by flipping bits –positive numbers unchanged –Example: +5 10 0000 0101 2 -5 10 1111 1010 2 Left-most bit still indicates sign

19 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)

20 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

21 Subtraction with One’s complement Evaluate 10–7 using 8-bit one’s complement arithmetic:

22 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 = 3 10

23 Two’s Complement Still have two zeros  two’s complement –Complement then add 1 –Our number range now asymmetric: -2 7 to 2 7 -1 –Used extra zero bit pattern

24 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)

25 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

26 Octal and Hexadecimal Each octal digit represents 3-bits Each hex digit represents 4-bits Examples: 13 10 = 1101 2 = (001)(101) 2 = 15 8 = (1101) 2 = D 16

27 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

28 Octal and Hexadecimal: Example 0100 0110 1101 0111 2

29 Octal and Hexadecimal: Example 0100 0110 1101 0111 2 = (000) (100) (011) (011) (010) (111) = 43327 8 = (0100) (0110) (1101) (0111) = 46D7 16 Note padding at front of number

30 Octal and Hexadecimal To convert from hex/octal to binary: reverse procedure FF 16 = (1111)(1111) 2 377 8 = (011)(111)(111) 2 NOTE: for fractional conversion, move from left to right and pad at right end: 0.11001101011 2 = 0. (110) (011) (010) (110) = 0.6326 8 0.11 2 = 0.(110) 2 = 0.6 8 Convert fractional/integer part separately


Download ppt "Number Systems and Logic Prepared by Dr P Marais (Modified by D Burford)"

Similar presentations


Ads by Google