1 / 29

Integer Representations & Bases

Integer Representations & Bases. Section 4.2 Wednesday 4 June. 1. Section Summary. Integer Representations Base b Expansions Binary Expansions Octal Expansions Hexadecimal Expansions Base Conversion Algorithm Algorithms for Integer Operations. Representations of Integers.

wardah
Télécharger la présentation

Integer Representations & Bases

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Integer Representations & Bases Section 4.2 Wednesday 4 June INTEGER REPRESENTATIONS & BASES 1

  2. Section Summary • Integer Representations • Base b Expansions • Binary Expansions • Octal Expansions • Hexadecimal Expansions • Base Conversion Algorithm • Algorithms for Integer Operations INTEGER REPRESENTATIONS & BASES

  3. Representations of Integers • Societies today use decimal notation, or base 10notation, to denote integers. • For example: 965 abbreviates 9∙102 + 6∙101 + 5∙100 . • But we could use any base b, where b is a positive integer greater than 1, to represent integers. • The bases b = 2 (binary), b = 8 (octal) , and b= 16 (hexadecimal) are important for computing and communications • The ancient Mayans used base 20 and the ancient Babylonians used base 60. INTEGER REPRESENTATIONS & BASES

  4. Why binary, octal, hex? • Physical representations for 0 and 1 • 0 volts v.s. 5 volts • lit v.s. dark • rough spot v.s. shiny spot (on CD surface) • etc. • Octal and hexidecimal are more compact and readable; easy to convert back and forth. INTEGER REPRESENTATIONS & BASES

  5. Base b Representations • Theorem 1: Let b be a positive integer greater than 1. If n is a positive integer, then n can be expressed uniquely in the form: n = akbk + ak-1bk-1 + …. + a1b + a0 where k is a nonnegative integer; a0, a1,…, ak are nonnegative integers less than b; and ak ≠ 0. • The integers aj in this theorem are called the base-b digits of the representation of n. • The representation of n in Theorem 1 is called the base b expansion of nand is denoted by (akak-1….a1a0)b. • We usually omit the subscript 10 for base 10 expansions. INTEGER REPRESENTATIONS & BASES

  6. Example: Binary Expansions • The binary, or base 2, expansion, uses the digits 0 and 1 only. (Why?) • Exercise: What decimal number equals (1 0101 1111)2? • (1 0101 1111)2 = 1∙28 + 0∙27 + 1∙26 + 0∙25 + 1∙24 + 1∙23 + 1∙22 + 1∙21 + 1∙20 =351. • Exercise: What decimal number equals (1 1011)2 ? • (11011)2 = 1 ∙24 + 1∙23 + 0∙22 + 1∙21 + 1∙20 = 27. INTEGER REPRESENTATIONS & BASES

  7. Example: Octal Expansions • The octal expansion (base 8) uses the digits {0,1,2,3,4,5,6,7}. • Exercise: What decimal number equals (7016)8? • 7∙83 + 0∙82 + 1∙81 + 6∙80 = 3598 • Exercise: What decimal number equals (111)8 ? • 1∙82 + 1∙81 + 1∙80 = 73 INTEGER REPRESENTATIONS & BASES

  8. Hexadecimal Expansions • The hexadecimal, or base 16, expansion uses {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} for digits. • (A)16 through (F)16 represent (10)10 through (15)10. • Exercise: What decimal number equals (2AE0B)16 ? • 2∙164 + 10∙163 + 14∙162 + 0∙161 + 11∙160 =175627 • Exercise: What decimal number equals (E5)16 ? • 1∙162 + 14∙161 + 5∙160 = 256 + 224 + 5 = 485 INTEGER REPRESENTATIONS & BASES

  9. Base Conversion To construct the base b expansion of an integer n: • Divide n by b to obtain a quotient q0 and remainder a0. n = b ∙ q0 + a0 0 ≤ a0 <b • The remainder, a0 , is the rightmost digit in the base b expansion of n. • Divide q0 by b to obtain a quotient q1 and remainder a1. q0 = b ∙ q1 + a1 0 ≤ a1 <b • The remainder, a1, is the second digit from the right in the base b expansion of n. • Continue by successively dividing the quotient by b, obtaining the next base-b digit as the remainder. • The process terminates when the quotient is 0. continued→ INTEGER REPRESENTATIONS & BASES

  10. Base Conversion: Why does this work? This process produces:n = b ∙ q0 + a0 0 ≤ a0 <b q0 = b ∙ q1 + a10 ≤ a1 <b q1 = b ∙ q2 + a20 ≤ a2 <b …… qk-2 = b ∙ qk-1 + ak-10 ≤ ak-1<b qk-1 = b ∙ 0 + ak0 ≤ ak<b • n = b ∙ q0 + a0 • = b ∙ (b ∙ q1 + a1) + a0 • = b2∙ q1 + b ∙ a1 + a0 • = b2 ∙ (b ∙ q2 + a2) + b ∙ a1 + a0 • = b3∙ q2 + b2∙a2 + b ∙ a1 + a0 … • = bk ∙ qk-1 + bk-1 ∙ak-1 + bk-2 ∙ak-2 + … … + b2∙ a2 + b ∙ a1 + a0 • = bk ∙ ak + bk-1 ∙ak-1 + bk-2 ∙ak-2 + … … + b2∙ a2 + b ∙ a1 + a0 • Thus, n = (ak ak-1 … a2 a1 a0)b INTEGER REPRESENTATIONS & BASES

  11. Algorithm: Constructing Base b Expansions • q represents the quotient obtained by successive divisions by b, starting with q = n. • The digits in the base b expansion are the remainders of the division given by qmodb. • The algorithm terminates when q = 0 is reached. procedurebase b expansion(n, b: positive integers with b > 1) q := n k := 0 while (q ≠ 0) ak := qmodb q := qdivb k := k + 1 return(ak-1 ,…, a1,a0) // {(ak-1 … a1a0)bis baseb expansion of n} INTEGER REPRESENTATIONS & BASES

  12. Base Conversion Exercise: Find the octal expansion of (12345)10 Solution: Successively dividing quotients by 8 gives: • 12345 = 8 ∙ 1543 + 1 (q1 = 1543, r1 = 1) • 1543 = 8 ∙ 192 + 7 (q2 = 192, r2 = 7) • 192 = 8 ∙ 24 + 0 (q3 = 24, r3 = 0) • 24 = 8 ∙ 3 + 0 (q4 = 3, r4 = 0) • 3 = 8 ∙ 0 + 3 (q5 = 0, r5 = 3) The remainders are the digits from right to left yielding (30071)8. INTEGER REPRESENTATIONS & BASES

  13. Base Conversion Exercise: Find the octal expansion of (12345)10 Solution: Successively dividing quotients by 8 gives: • q1 = 12345 div 8 = 1543, a1 = 12345 mod 8 = 1 • q2 = 1543 div 8 = 192, a2 = 1543 mod 8 = 7 • q3 = 192 div 8 = 24, a3 = 192 mod 8 = 0 • q4 = 24 div 8 = 3, a4 = 24 mod 8 = 0 • q5 = 3 div 8 = 0, a5 = 3 mod 8 = 3 The remainders are the digits from right to left yielding (30071)8. INTEGER REPRESENTATIONS & BASES

  14. Comparison of Hexadecimal, Octal, and Binary Representations Initial 0s are not shown When padded with initial 0’s: Each octal digit corresponds to a block of 3 binary digits. Each hexadecimal digit corresponds to a block of 4 binary digits. This makes conversion between binary, octal, and hexadecimal easy. INTEGER REPRESENTATIONS & BASES

  15. Conversions between binary and octal • Binary to octal: replace 3-bit blocks with octal digit • Example: (11111010111100)2 (011 111 010 111 100)2 ( 3 7 2 7 4 )8 • Octal to binary: replace octal digit with 3-bit binary equivalent • Example: (37274)8 ( 3 7 2 7 4 )8 (011 111 010 111 100)2 INTEGER REPRESENTATIONS & BASES

  16. Conversions between binary and hex • Binary to octal: hexidecimal: replace 4-bit blocks with hexidecimal digit • Example: (11111010111100)2 (0011 1110 1011 1100)2 ( 3 E B C )16 • Hexidecimal to binary: replace hexidecimal digit with 4-bit blocks with binary block • Example: (3EBC)16 ( 3 E B C )16 (0011 1110 1011 1100)2 INTEGER REPRESENTATIONS & BASES

  17. Addition of Base-b Integers • Addition of decimal numbers generalizes for any base • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal sum n1 + n2 + … + nkto base-b • Example: (0)2 + (1)2 = (1)2 since 0 + 1 = 1 = (1) 2 • Example: (1)2 + (1)2 = (10)2 since 1 + 1 = 2 = (10) 2 • Example: (1)2 + (1)2 + (1)2 = (11)2 since 1 + 1 + 1 = 3 = (11) 2 • Calculate the sum of the base-b digits from right to left, potentially carrying a bit. • Example: 1 1 carry bits (1 1 1 0 )2 + (1 0 1 1 )2 (1 1 0 0 1 )2 17 MSU/CSE 260 Fall 2009

  18. Addition of Base-b Integers • Addition of decimal numbers generalizes for any base • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal sum n1 + n2 + … + nkto base-b • Example: (0)2 + (1)2 = (1)2 since 0 + 1 = 1 = (1) 2 • Example: (1)2 + (1)2 = (10)2 since 1 + 1 = 2 = (10) 2 • Example: (1)2 + (1)2 + (1)2 = (11)2 since 1 + 1 + 1 = 3 = (11) 2 • Calculate the sum of the base-b digits from right to left, potentially carrying a bit. • Example: 1 1 carry bits (1 1 1 0 )2 (14)10 + (1 0 1 1 )2 (11)10 (1 1 0 0 1 )2 (25)10 18 MSU/CSE 260 Fall 2009

  19. Addition of Base-b Integers • Addition of decimal numbers generalizes for any base • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal sum n1 + n2 + … + nkto base-b • Example: (3)8 + (7)8 + (1)8 = (13)8 since 3 + 7 + 1 = 11 = (13) 8 • Calculate the sum of the base-b digits from right to left, potentially carrying a bit. • Example: 1 1 carry bits (1 3 5 1 )8 + (1 7 3 4 )8 (3 3 0 5 )8 19 MSU/CSE 260 Fall 2009

  20. Addition of Base-b Integers • Addition of decimal numbers generalizes for any base • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal sum n1 + n2 + … + nkto base-b • Example: (3)8 + (7)8 + (1)8 = (13)8 since 3 + 7 + 1 = 11 = (13) 8 • Calculate the sum of the base-b digits from right to left, potentially carrying a bit. • Example: 1 1 carry bits (1 3 5 1 )8 (745)10 + (1 7 3 4 )8 (988)10 (3 3 0 5 )8 (1733)10 20 MSU/CSE 260 Fall 2009

  21. Exercise Calculate the following sums: (1100110)2 (60352)8 (A750C)16 +( 101111)2 +(32277)8 +(B9553)16 (10010101)2 (112651)8 (160A5F)16 INTEGER REPRESENTATIONS & BASES

  22. Multiplying Base-b Integers • Multiplication of base-b numbers is similarly based on the usual method for multiplying decimal numbers. • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal product n1∙n2∙ … ∙nkto base-b Example: (0)2∙ (0)2 = (0)2∙ (1)2 = (1)2∙ (0)2 = (0)2 (1)2∙ (1)2 = (1)2 • Example (1 1 0)2  (1 0 1)2 (1 1 0)2 (0 0 0)2 + (1 1 0)2___ (1 1 1 1 0)2 • Up to 2n bits may be needed to represent the product of two n-bit numbers. 22 MSU/CSE 260 Fall 2009

  23. Multiplying Base-b Integers • Multiplication of base-b numbers is similarly based on the usual method for multiplying decimal numbers. • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal product n1∙n2∙ … ∙nkto base-b Example: (0)2∙ (0)2 = (0)2∙ (1)2 = (1)2∙ (0)2 = (0)2 (1)2∙ (1)2 = (1)2 • Example (1 1 0)2 (6)10  (1 0 1)2 (5)10 (1 1 0)2 + (0 0 0)2 (1 1 0)2___ (1 1 1 1 0)2 (30)10 • Up to 2n bits may be needed to represent the product of two n-bit numbers. 23 MSU/CSE 260 Fall 2009

  24. Multiplying Base-b Integers • Multiplication of base-b numbers is similarly based on the usual method for multiplying decimal numbers. • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal product n1∙n2∙ … ∙nkto base-b Example: (3)8∙ (6)8 = (22)8 since 3∙6 = 18 = (22)8 • Example (7 6 1)8  ( 2 3)8 (2 7 2 3)8 + (1 7 4 2)8 (2 2 3 4 3)8 • Up to 2n bits may be needed to represent the product of two n-bit numbers. 24 MSU/CSE 260 Fall 2009

  25. Multiplying Base-b Integers • Multiplication of base-b numbers is similarly based on the usual method for multiplying decimal numbers. • For base-b digits, n1, n2,… nk ∈ { 0, 1, 2, …, b − 1 }, convert the decimal product n1∙n2∙ … ∙nkto base-b Example: (3)8∙ (6)8 = (22)8 since 3∙6 = 18 = (22)8 • Example (7 6 1)8 (497)10  ( 2 3)8 (19)10 (2 7 2 3)8 + (1 7 4 2)8 (2 2 3 4 3)8 (9443)10 • Up to 2n bits may be needed to represent the product of two n-bit numbers. 25 MSU/CSE 260 Fall 2009

  26. Exercise Calculate the following products: (10110)2 (705)8 (78)16 × (110)2 × (52)8 × (2B)16 INTEGER REPRESENTATIONS & BASES

  27. Exercise Calculate the following products: (10110)2 (705)8 (78)16 × (110)2 × (52)8 × (2B)16 (00000)2 (10110)2 (10110)2 (10000100)2 INTEGER REPRESENTATIONS & BASES

  28. Exercise Calculate the following products: (10110)2 (705)8 (78)16 × (110)2 × (52)8 × (2B)16 (00000)2 (10110)2 (10110)2 (10000100)2 (1612)8 (4331)8 (45122)8 INTEGER REPRESENTATIONS & BASES

  29. Exercise Calculate the following products: (10110)2 (705)8 (78)16 × (110)2 × (52)8 × (2B)16 (00000)2 (10110)2 (10110)2 (10000100)2 (1612)8 (4331)8 (45122)8 (528)16 (F0)16 (1428)16 INTEGER REPRESENTATIONS & BASES

More Related