number representation
play

Number representation A number can be represented in binary in many - PowerPoint PPT Presentation

Number representation A number can be represented in binary in many ways. The most common number types to be represented are: Integers, positive integers one-complement, two-complement, sign-magnitude Decimal real numbers with a fixed


  1. Number representation A number can be represented in binary in many ways. The most common number types to be represented are: • Integers, positive integers one-complement, two-complement, sign-magnitude • Decimal real numbers with a fixed range fixed-point • Decimal real numbers floating-point William Sandqvist william@kth.se

  2. Integers Positive integers: 2 6 2 5 2 4 2 3 2 2 2 1 2 0 = 1 ⋅ 2 6 + 1 ⋅ 2 5 + 1 ⋅ 2 3 + 1 ⋅ 2 2 + 1 ⋅ 2 0 = 109 1 1 0 1 1 0 1 But how to represent negative numbers ??? William Sandqvist william@kth.se

  3. Sign-magnitude Integer: 2 5 2 4 2 3 2 2 2 1 2 0 S = - (1 ⋅ 2 5 + 1 ⋅ 2 3 + 1 ⋅ 2 2 + 1 ⋅ 2 0 ) = - 45 1 1 0 1 1 0 1 The magnitude of the number Sign-bit Drawback: 1 0 0 0 0 0 0 two representations 0 0 0 0 0 0 0 of zero (+/-) 0 William Sandqvist william@kth.se

  4. 1-complement The negative numbers are the complement of the positive numbers. Bit for bit in the positive number is inverted to get the corresponding negative. B= b N-1 b N-2 ...b 1 b 0 där b i ∈ { 0,1} b N-1 b N-2 ... b 1 b 0 Drawbacks: Sign Bit • Two zeroes (+/-) 0. • At some additions adjusting is required. William Sandqvist william@kth.se

  5. 2-complement Representation with 2-complement B= b N-1 b N-2 ...b 1 b 0 där b i ∈ { 0,1} b N-1 b N-2 ... b 1 b 0 Sign Bit Decimalvalue: D(B)= - b N-1 2 N-1 + b N-2 2 N-2 + ...+ b 1 2 1 + b 0 2 0 This is the most common representation of signed integers. William Sandqvist william@kth.se

  6. 2-complement Conversion example: B= b N-1 b N-2 ...b 1 b 0 där b i ∈ { 0,1} 1 1 1 1 1 1 1 1 Sign Bit Decimalvalue: D(B)= - b N-1 2 N-1 + b N-2 2 N-2 + ...+ b 1 2 1 + b 0 2 0 It’s alwais the biggest number that = -128 + 127 = -1 corresponds to -1. William Sandqvist william@kth.se

  7. Number conversion positive number to negative The Twocomplement methood 01111 +15 10000 invert 10001 add one 10001 -15 William Sandqvist william@kth.se

  8. Number conversion negative number to positive Twocomplement methood 10001 -15 01110 invert 01111 add one 01111 +15 The same procedure in both directions! William Sandqvist william@kth.se

  9. 2-complement 000 111 0 001 1 -1 Computer registers are "rings". The figure shows a three-bit register. 010 110 -2 2 When you count with signed numbers the negative -3 3 numbers, are the left 011 101 -4 half of the ring. 100 William Sandqvist william@kth.se

  10. 2-complement ”fast conversion” • In order to easily produce 2's complement of a binary number, you can use the following procedure: – Start fron the right side – Copy all bits from the binary number that is 0 and the first bit that is 1. Invert – Invert the rest of the bits. Copy Exemple: 2-complement from 110 is 010 William Sandqvist william@kth.se

  11. Sign-extension In calculations the computers often need to increase the number of digits (bits) before any calculation - how do you do it with negative numbers? Integer: -2 6 2 5 2 4 2 3 2 2 2 1 2 0 = -1 ⋅ 2 6 + 1 ⋅ 2 5 +1 ⋅ 2 3 + 1 ⋅ 2 2 + 1 ⋅ 2 0 = - 45 1 1 0 1 1 0 1 -2 n-1 2 n-2 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 1 1 1 1 1 0 1 1 0 1 Sign bit has a negative weight If you want to extend the number of digits you need to copy the sign bit to all extended bits! William Sandqvist william@kth.se

  12. Addition (BV: page 264) 0101 (+5) + 0010 + (+2) (+7) 0111 William Sandqvist william@kth.se

  13. Addition (BV: page 264) ignore 1 1 0101 (+5) + 1110 + (-2) (+3) 1 0011 Carry-bit could be ignored! William Sandqvist william@kth.se

  14. Addition (BV: page 264) ignore 1 1 1 1011 (-5) + 1110 + (-2) (-7) 1 1001 Carry-bit could be ignored! William Sandqvist william@kth.se

  15. Overflow Overflow – the sign bit is not consistent with the input numbers 1 1 0101 (+5) + 0101 + (+5) (-6) 1010 William Sandqvist william@kth.se

  16. Overflow 2 Overflow – the sign bit is not consistent with the input numbers ignore 1 1 1 1011 (-5) + 1011 + (-5) (+6) 1 0110 Carry-bit could be ignored! William Sandqvist william@kth.se

  17. Subtraction (BV: page 265) ”Borrow” one 10 0101 (+5) - 0010 - (+2) (+3) ???? How do you do the subtraction in an easy way? William Sandqvist william@kth.se

  18. Subtraction (BV: page 265) ignore 1 1 0101 0101 (+5) - 0010 + 1110 - (+2) (+3) ???? 0011 1 Carry-bit could be ignored! Making an addition of 2's complement instead! William Sandqvist william@kth.se

  19. Subtraction (BV: page 265) ignore 1 1 1 1011 1011 (-5) - 0010 + 1110 - (+2) (-7) ???? 1001 1 Carry-bit could be ignored! William Sandqvist william@kth.se

  20. Subtraction (BV: page 265) 1011 0101 (+5) - 1110 + 0010 - (-2) (+7) ???? 0111 William Sandqvist william@kth.se

  21. Subtraction (BV: page 265) 1 1011 1011 (-5) - 1110 + 0010 - (-2) (-3) ???? 1101 William Sandqvist william@kth.se

  22. 2-complement summary • Range : -2 N-1 up to 2 N-1 - 1 • Negation : Invert every bit (the boolean complement), and add 1. • Expansion of bit-length : Extend with bits at the left of the number with the same value as the sign bit. • Overflow-rule : If two numbers with the same sign are added, it has become overflow if the result has the opposite sign. • Subtraction rule : To subtract B from A, take the two complement of B and add to A. William Sandqvist william@kth.se

  23. Alternative way to detect overflow (BV: page 271) c 3 =1 c 4 =0 0 1 1 0111 (+7) + 0010 + (+2) (-7) 1001 Overflow because c 4 and c 3 are different! William Sandqvist william@kth.se

  24. Alternative way to detect overflow (BV: page 271) c 3 =0 c 4 =0 0 0 1001 (-7) + 0010 + (+2) (-5) 1011 Not Overflow because c 4 and c 3 are the same! William Sandqvist william@kth.se

  25. Alternative way to detect overflow (BV: page 271) c 3 =1 c 4 =1 ignore 1 1 1 0111 (+7) + 1110 + (-2) (+5) 1 0101 Carry-bit could be ignored! Not Overflow because c 4 and c 3 are the same! William Sandqvist william@kth.se

  26. Alternative way to detect overflow (BV: page 271) c 3 =0 c 4 =1 ignore 1 0 1001 (-7) + 1110 + (-2) (+7) 1 0111 Carry-bit could be ignored! Overflow because c 4 and c 3 are different! William Sandqvist william@kth.se

  27. Logic to detect overflow XOR For 4-bit-number Overflow if c 3 och c 4 are different Otherwise, it is not overflow 4 + c 3 c 4 = c 3 ⊕ c 4 Overflow = c 3 c For n -bit-number Overflow = c n − 1 ⊕ c n William Sandqvist william@kth.se

  28. Quickie Question … The number +5 is represented by 0101 if we use 4 bits. What number is equivalent to -5 if we use 8-bit two's complement representation? a: 1111 1011 b: 1111 1010 c: 1000 0101 William Sandqvist william@kth.se

  29. Hardware for arithmetic William Sandqvist william@kth.se

  30. a s Half adder HA b c a b c s a a 0 0 0 0 b b 0 1 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 0 c = a ⋅ b s = a ⊕ b a s b c William Sandqvist william@kth.se

  31. a s Full adder FA b c ut c in a b c in c ut s c in 00 01 11 10 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 0 1 1 1 0 s = a ⊕ b ⊕ c in 1 0 0 0 1 1 0 1 1 0 a b ab s 1 1 0 1 0 c in c in 00 01 11 10 1 1 1 1 1 0 0 0 1 0 c ut 1 0 1 1 1 c ut = a b + c in a + c in b William Sandqvist william@kth.se

  32. The sum function? c in 00 01 11 10 0 0 1 0 1 1 1 0 1 0 s = a ⊕ b ⊕ c in William Sandqvist william@kth.se

  33. Sum = Odd parity The Full Adder sum function is the "odd" parity function. This is the XOR function’s natural extension to more variables than two. Odd parity is when the number of 1’s on the inputs is an odd number. Odd parity XOR a 0 1 b a ⊕ b 0 1 0 1 0 1 Odd Odd parity ab parity a ⊕ b ⊕ c c 00 01 11 10 0 0 1 0 1 1 1 0 1 0 William Sandqvist william@kth.se

  34. Carry function? 00 01 11 10 0 0 0 1 0 1 0 1 1 1 • Majority function . Output assumes same value 1/0 as a majority of the inputs. M William Sandqvist william@kth.se

  35. Full-adder with two ½ -adders We may also construct a full-adder with two half-adders and an OR gate. ≡ a a s FA s b b c ut HA c ut c in c in HA Decomposition means that you see the circuit as composed of building blocks. Such known building blocks could be used to build build entirely new systems, Composition . William Sandqvist william@kth.se

  36. Full-adder ½+ ½ = 1 a ⋅ b ab a b c in c ut s c in 00 01 11 10 c in 0 0 0 0 0 s a HA c ut 0 0 0 1 0 1 HA 0 0 1 0 b 0 1 0 0 1 1 0 1 1 1 0 1 1 1 0 c in s 1 0 0 0 1 (a ⊕ b) ⋅ c in a 1 0 1 1 0 c ut = (a ⊕ b) ⋅ c in + a ⋅ b b 1 1 0 1 0 c ut 1 1 1 1 1 ab c in 00 01 11 10 ( a ⊕ b ) can be shared for 0 0 1 0 1 both s and c ut ! 1 1 0 1 0 s = a ⊕ b ⊕ c in William Sandqvist william@kth.se

  37. Popular tattoo? Tattoos are forever! Unfortunately this is not the "best" adder, not if you want fast computers. Exciting continuation of adder circuits follow ... William Sandqvist william@kth.se

  38. ( Parityfunction – Three-way light control ) c in 00 01 11 10 Odd parity. 0 0 1 0 1 1 1 0 1 0 s = a ⊕ b ⊕ c in William Sandqvist william@kth.se

Recommend


More recommend