1 / 85

Unary, Binary, and Beyond

Unary, Binary, and Beyond. Last time we talked about unary notation. n th Triangular Number.  n = 1 + 2 + 3 + . . . + n-1 + n = n(n+1)/2. n th Square Number.  n = 1 + 3 + … + 2n-1 = Sum of first n odd numbers. n th Square Number.  n =  n +  n-1

mariame
Download Presentation

Unary, Binary, and Beyond

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. Unary, Binary, and Beyond

  2. Last time we talked about unary notation.

  3. nth Triangular Number • n = 1 + 2 + 3 + . . . + n-1 + n • = n(n+1)/2

  4. nth Square Number • n = 1 + 3 + … + 2n-1 • = Sum of first n odd numbers

  5. nth Square Number • n = n + n-1 • = n2

  6. (n)2 = + + . . . + n (n)2 =(n-1)2 + n

  7. Unary takes size n to represent the number n. Higher bases give a logarithmically more compact representation.

  8. Base X, X>1 • A base X digit is any integer 0 · a < X • Let S = an-1, an-2, …, a0 be a sequence of base X digits. S is said to be the base X representation of the number: • an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

  9. Base 2 = Binary Base 10 = Decimal • A base X digit is any integer 0 · a < X • Let S = an-1, an-2, …, a0 be a sequence of base X digits. S is said to be the base X representation of the number: • an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

  10. Sequence S = an-1, an-2, …, a0 represents the number: an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 • Base 2 • 101 represents 1 (2)2 + 0 (21) + 1 (20) • Base 7 • 015 represents 0 (7)2 + 1 (71) + 5 (70)

  11. Sequence S = an-1, an-2, …, a0 represents the number: an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 • Base 2 • 101 represents 1 (2)2 + 0 (21) + 1 (20) • = • Base 7 • 015 represents 0 (7)2 + 1 (71) + 5 (70) • =

  12. Biggest n “digit” number in base X would be:(X-1)Xn-1 + (X-1)Xn-2-1+…(X-1)X0 • Base 2 • 111 represents 1 (2)2 + 1 (21) + 1 (20) • Base 7 • 666 represents 6 (7)2 + 6 (71) + 6 (70)

  13. Biggest n “digit” number in base X would be:(X-1)Xn-1 + (X-1)Xn-2-1+…(X-1)X0 • Base 2 • 111 represents 1 (2)2 + 1 (21) + 1 (20) • 111 + 1 = 1000 represents 23 • Thus, 111 represents 23 – 1 • Base 7 • 666 represents 6 (7)2 + 6 (71) + 6 (70) • 666 + 1 = 1000 represents 73 • Thus, 666 represents 73 - 1

  14. Biggest n “digit” number in base X would be:S= (X-1)Xn-1 + (X-1)Xn-2-1+…(X-1)X0\Add 1 to get: Xn + 0 Xn-1 + … + 0 X0Thus, S = Xn - 1 • Base 2 • 111 represents 1 (2)2 + 1 (21) + 1 (20) • 111 + 1 = 1000 represents 23 • Thus, 111 represents 23 – 1 • Base 7 • 666 represents 6 (7)2 + 6 (71) + 6 (70) • 666 + 1 = 1000 represents 73 • Thus, 666 represents 73 - 1

  15. (X-1)Xn-1 + (X-1)Xn-2-1+…(X-1)X0 = Xn - 1 • Factoring out (x-1). We obtain: • (X-1) (Xn-1 + Xn-2 + ….. +X + 1) = Xn – 1 • Dividing by X-1 (since X is not equal to 1): • (Xn-1 + Xn-2 + ….. +X + 1) = (Xn – 1) / (X -1) • Or. • (1 + X + X2 + … + Xn-1) = (Xn – 1) / (X -1)

  16. Xn – 1 1 + X1 + X2 + X 3 + … + Xn-2 + Xn-1 = X- 1 This is a super important sum called the Geometric Series (or Sum). Let’s prove this again, a different way. . .

  17. An Important Calculation • (X-1) ( 1 + X1 + X2 + X 3 + … + Xn-2 + Xn-1 ) • = X1 + X2 + X 3 + … + Xn-1 + Xn • - 1 - X1 - X2 - X 3 - … - Xn-2 – Xn-1 • = - 1 + Xn • = Xn - 1

  18. The Geometric Series • (X-1) ( 1 + X1 + X2 + X 3 + … + Xn-1 ) = Xn - 1 Xn – 1 1 + X1 + X2 + X 3 + … + Xn-2 + Xn-1 = X- 1

  19. Xn+1 - 1 1 + X1 + X2 + X 3 + … + Xn-1 + Xn = X- 1

  20. The highest n digit number in base X. • (X-1) ( 1 + X1 + X2 + X 3 + … + Xn-1 ) = Xn - 1 Base X. Let S be the sequence of n digits each of which is X-1. S is the largest number of length n that can be represented in base X. S = Xn - 1

  21. Notice that we have not yet shown that each base-b number has a unique representation. In service of this, we will introduce a variation of base-b called “plus minus base-b”.

  22. Plus/Minus Base X • An plus/minusbase X digit is any integer –X < a < X • Let S = an-1, an-2, …, a0 be a sequence of plus/minus base X digits. S is said to be the plus/minus base X representation of the number: • an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0

  23. Does each n-digit number in plus/minus base X represent a different integer?

  24. No. Consider plus/minus binary. 5 = 0 1 0 1 = 1 0 -1 -1

  25. Humm.. So what is it good for?

  26. Not every number has a unique plus/minus binary representation, but 0 does.

  27. 0 has a unique n-digit plus/minus base X representation as all 0’s. • Suppose an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 = S =0, where there is some highest k such that ak 0. Wl.o.g. assume ak > 0. • Because Xk > (X-1)(1 + X1 + X2 + X 3 + … + Xk-1) • no sequence of digits from ak-1 to a0 can represent a number as small as –Xk • Hence S  0. Contradiction.

  28. Each of the numbers from 0 to Xn-1 is uniquely represented by an n-digit number in base X. • We already know that n–digits will represent something between 0 and Xn – 1. • Suppose two distinct sequences represent the same number: • an-1 Xn-1 + an-2 Xn-2 + . . . + a0 X0 = • bn-1 Xn-1 + bn-2 Xn-2 + . . . + b0 X0 • The difference of the two would be an plus/minus base X representation of 0, but it would have a non-zero digit. Contradiction.

  29. Each of the numbers from 0 to Xn-1 is uniquely represented by an n-digit number in base X. • n digits represent up to Xn – 1 • n-1 digits represents up to Xn-1 - 1 • Let k be a number: Xn-1 ≤ k ≤ Xn - 1 • So k can be represented with n digits. • For all k:  logxk  = n – 1 • So k uses  logxk  + 1 digits.

  30. Fundamental Theorem For Base X:Each of the numbers from 0 to Xn-1 is uniquely represented by an n-digit number in base X. • k uses  logxk  + 1 digits in base X.

  31. n has length n in unary, but has length  log2n  + 1 in binary. Unary is exponentially longer than binary.

  32. Bases In Different Cultures • Sumerian-Babylonian: 10, 60, 360 • Egyptians: 3, 7, 10, 60 • Africans: 5, 10 • French: 10, 20 • English: 10, 12,20

  33. Egyptian Multiplication The Egyptians used decimal numbers but multiplied and divided in binary

  34. Egyptian Multiplication a times bby repeated doubling • b has some n-bit representation: bn..b0 • Starting with a, repeatedly double largest so far to obtain: a, 2a, 4a, …., 2na • Sum together all 2ka where bk = 1

  35. Egyptian Multiplication 15 times 5by repeated doubling • 5 has some 3-bit representation: 101 • Starting with 15, repeatedly double largest so far to obtain: 15, 30, 60 • Sum together all 2k(15) where bk = 1: • 15 + 60 = 75

  36. Why does that work? • b = b020 + b121 + b222 + … + bn2n • ab = b020a + b121a + b222a + … + bn2na • If bk is 1 then 2ka is in the sum. • Otherwise that term will be 0.

  37. Wait! How did the Egyptians do the part where they converted b to binary?

  38. They used repeated halving to do base conversion. Consider

  39. Egyptian Base Conversion • Output stream will print right to left. • Input X. • Repeat until X=0 • { • If X is even then Output O; • Otherwise {X:=X-1; Output 1} • X:=X/2 • }

  40. Egyptian Base Conversion • Output stream will print right to left. • Input X. • Repeat until X=0 • { • If X is even then Output O; • Otherwise Output 1 • X:= X/2 • }

  41. Start the algorithm 010101 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 1

  42. Start the algorithm 01010 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 1

  43. Start the algorithm 01010 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 01

  44. Start the algorithm 0101 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 01

  45. Start the algorithm 0101 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 101

  46. Start the algorithm 010 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 101

  47. And Keep Going until 0 0 • Repeat until X=0 • { If X is even then Output O; Otherwise Output 1; • X:= X/2 • } 010101

  48. Sometimes the Egyptian combined the base conversion by halving and the multiplication by doubling into one algorithm

  49. 70 140 280 560 * 70 6 * 350 1 * 910 Rhind Papyrus (1650 BC)70*13

  50. 70 140 280 560 * 70 6 * 350 1 * 910 Rhind Papyrus (1650 BC)70*13 Binary for 13 is 1101 = 23 + 22 + 20 70*13 = 70*23 + 70*22 + 70*20

More Related