<<

CSE 1400/MTH 2051 Discrete Math Notes William David Shoaff August 30, 2017

Preliminaries

• Read and understand the syllabus • Use Canvas to communicate and review grades • Use my page (cs.fit.edu/~wds/classes/adm) for supplemental material • Follow the calendar: Listen for announced calendar changes. • Recitation sessions: Tuesdays & Thursday, 5:30 p.m. to 7:30 p.m. in Olin Engineering 137 • Edward Tufte recommends this lecture structure: – Provide audience 1 to 2 pages of notes – Allow audience 10 to 20 to read notes – Glare at those who are not reading (his wry sense of humor?) – While fleshing-out notes ask and answer questions cse 1400/mth 2051 discrete math notes 2

Numbers

Numbers are very useful. Numbers can be written in many different languages. The language, base 10, is the lingua franca for common conversations. The decimal alphabet is

D = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} called digits

These digits provide coefficients for powers of 10, where the power p is determined by the position of the 10. An example should help:

314.15 = 3 · 102 + 1 · 101 + 4 · 100 + 1 · 10−1 + 5 · 10−2

The syntax (position) of the digit determines the semantics (meaning) of the expression. A breakthrough occurred in the late first-half of the twentieth century when electronic com- puters were built. The design of these machines made the binary language, base 2, important. The binary alphabet is B = {0, 1} called These bits provide coefficients for powers of 2. The power p is positional. (Do you find it interesting that 2 is

written as (10)2 in base 2?, I do, there is a pattern. 16 written in base 16 is (10)16.) When written in binary, the value 314.15 is

=  314.15 1 0011 1010.001 0011 0011 2 where 0011 repeats without bound

One goal of the class is to master techniques for conversions between these two languages. First, agree that by , 8 5 4 3 1 314 = (1 0011 1010)2 = 2 + 2 + 2 + 2 + 2 (rational numbers) will be studied later. Numbers written in binary are long, about lg 10 ≈ 3.3219 ··· longer than decimal. To write shorter strings, use larger alphabets. The early 70’s Intel 4004 CPU was a 4- chip. Four bits are sometimes called a nybble. Four bits define digits (hexits). The hexadecimal language, base 16 uses the alphabet

H = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, , , E, F} called hexits

The letters A to F stand for the numbers 10 to 15. Now the value equivalence is

2 314 = (13A)16 = 16 + 3 · 16 + 10

Numbers written in hexadecimal are 4 = lg 16 shorter than numbers written in binary and about

1.2 = log10 16 times shorter than numbers written in decimal.

Exponential – Logarithmic Map between Value and String Length

There is an exponential – logarithmic relationship between the value of a n and the length of its

representation in a given base. Approximately logb n numerals are needed to write (n)b. The exact formula is   logb n + 1 = count of numerals required to write n in base b. cse 1400/mth 2051 discrete math notes 3

An example is a start at understanding:   log10 314 + 1 = b≈ 2.4 ···c + 1 = 3 the number of digits to write 314.

Nothing is important! Starting from nothing, zero, you can count up to 10k − 1 using k digits. For instance, you can count from 0 to 99 using at most 2 digits. Similarly, you can count from 0 to (111)2 using at most 3 bits. As a general statement: You can count from 0 to bk − 1 using at most k base b numerals.

Historical number systems are interesting. The system, base 60, remains in use today. Babylonians made sexagesimal famous. They learned it from the Sumerians. (This was about 4000 to 5000 years ago) There are 60 minutes in an ; 60 in a . There are 360 degrees in a circle, 60 minutes of arc in a . The number 60 = 223151 is highly composite: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 all divide 60. Properties of and composite numbers are studied in number theory. A topic with inter- esting uses. Given the prime factorization of n, being able to count n’s is a useful skill too. I find it interesting that the Romans are known for their engineering, but, their number system The Roman alphabet is {I, V, X, L, C, D, M}. Arcane rules define the value interpretation (semantics) from the format (syntax) of a number written using . Finally, as a teacher, I like to ask questions about numbers in arbitrary bases, say base 3 or 11, to test if students understand the ideas.

Questions and Problems

1. What is the alphabet for base 3, ternary numbers? 2. What is the alphabet for base 8, numbers? 3. If you were to use digits and lower case English alphabet to represent numbers, what would the base be? 4. Are Roman numerals a positional number system?

5.L ogarithms are important! The expression lg n stands for the log base 2 of n. What are the values of lg 1, lg 2, lg 16, lg 256? 6. What does it mean to say a number p is prime? 7. What does it mean to say a number c is composite? 8. What does it mean to say a is a of n? 9. Describe the prime factorization of a number.

10.L anguages are important! How would you define language? Syntax and semantics are words about languages. How would you define syntax and how would you define semantics? cse 1400/mth 2051 discrete math notes 4

Conversions Among Number Systems

Language translation is an interesting topic. Natural language translation is a difficult problem. It is being solved incrementally. Translations between number systems is algorithmic. There is one efficient , and its inverse. The algorithm is known as Horner’s rule, but everyone knows her as synthetic . To be sure, there are other conversion and efficiencies in special cases.

Horner’s rule

Horner’s rule converts numbers written in base b into numbers written in base c, using base c arithmetic. You are proficient in base 10 decimal arithmetic. By hand, Horner’s rule is used to change base b numbers into base 10 (decimal) numbers. Here are three examples:

Show that the (0010 1010)2 = 42. Horner’s Rule @ x = 2 0 0 1 0 1 0 1 0 0 0 2 4 10 20 42 0 0 1 2 5 10 21 42

Show that the binary number (1100 1100)2 = 204. Horner’s Rule @ x = 2 1 1 0 0 1 1 0 0 2 6 12 24 50 102 204 1 3 6 12 25 51 102 204

Show that the octal number (237)8 = 159. Horner’s Rule @ x = 8 2 3 7 16 152 2 19 159

n Horner’s schema can be expressed by the code below. The code evaluates the polynomial p(x) = an x + n−1 an−1 x + ··· + a1 x + a0 at the value of the base b. That is, the code computes the value

p(b) = (an an−1 ··· a1 a0 )b

horner : : (Num a ) => a −> [ a ] −> a horner b [ ] = 0 horner b [a] = a horner b [a:a1 ] = a *b + a1 horner b [a:a1 :as] = horner b [a *b+a1 : as ] If you pretend numerals are stored in little-endian order, that is reversed of the usual order, so the less sig- nificant numeral is first: (1234)le = 4321, then replace the line horner x [b:c:ds] = horner x [b*x+c:ds] with horner x (b:bs) = b + x * (horner x bs) What’s clear from the code is: It takes n multiplies and n adds to convert a number (a0a1 ··· an − 1an)b to decimal. cse 1400/mth 2051 discrete math notes 5

Inverting Horner’s rule

Horner’s rule uses ; it should not be surprising that its inverse uses divi- sion. By hand, the inverse of Horner’s rule converts a decimal number to some other base. Here are three examples: • Convert the decimal number 161 to binary.

Repeated Remaindering mod 2 161 80 40 20 10 5 2 1 Remainders 1 0 0 0 0 1 0 1

Therefore 161 = (1010 0001)2. • Convert the decimal number 237 to binary.

Repeated Remaindering mod 2 Quotients 237 118 59 29 14 7 3 1 Remainders 1 0 1 1 0 1 1 1

Therefore 237 = (1110 1101)2. • Convert the decimal number 73 to octal.

Repeated Remaindering mod 8 Quotients 73 9 1 Remainders 1 1 1

Therefore 73 = (111)8. Inverting Horner’s schema (repeated remaindering) can be expressed by the code below. The code con- verts a decimal number a into its representation as a list of base b numerals. remaindering :: ( I n t e g r a l a ) => a −> a −> [ a ] remaindering b 0 = [ ] remaindering b a = rem a b : remaindering b ( quot a b )

Questions and Problems

1. Choose some small numbers, 2 or 3 digits, and convert them to binary or octal. 2. Choose some small binary or octal numbers, and convert them to decimal cse 1400/mth 2051 discrete math notes 6

Binary – Octal – Hexadecimal Conversion

Powers of 2 form the basis for binary numbers. The basis for hexadecimal numbers are powers of 16, and 16 = 24 is also a power of 2. Because of this, grouping or expanding by 4’s leads to efficient conversions. For instance,

(2ABF)16 = (0010)2 (1010)2 (1011)2 (1111)2 = (0010 1010 1011 1111)2

Questions and Problems

1. Test your ability: Write several 4 bit numbers. Convert them to decimal. Convert them to hexadecimal. 2. What is the meaning of the word basis in the phrase: “The basis for binary numbers are powers of 2.”? 3. Is it right-to-left or left-to-right grouping used when converting binary to hexadecimal? Does it matter? 4. Describe algorithms for convert binary to octal and octal to binary.

Negative Numbers

Not everything is positive. The natural numbers

N = {0, 1, 2, . . .}

are useful for (and multiplication): Addition (and multiplication) are closed on N, but is not closed on N. To support subtraction, the naturals are expanded to integers.

Z = {0, ±1, ±2, . . .}

Except for 0, every integer has a + or − sign, and its sign must be encoded somehow. The usual rule for subtraction is arcane:  a − b if a ≥ b a − b = −(b − a) otherwise

For instance 10 − 5 = 5 and 5 − 10 = −(10 − 5). To avoid these cases an alternative number representation can be used.

Ten’s Complement

Ten’s complement are introduced to help understand two’s complement. Ten’s complement numbers represent a range of integers. The number of digits used determines the range. • One digit can represent [−5, 4]: The ten’s complement system uses 0 to 4 as normal and uses 5 to 9 to represent −5 to −1. That is,

(5)10c = 5 − 10 = −5 (6)10c = 6 − 10 = −4 (7)10c = 7 − 10 = −3

(8)10c = 8 − 10 = −2 (9)10c = 9 − 10 = −1 cse 1400/mth 2051 discrete math notes 7

• Two digits can represent [−50, 49]: The numbers (00)10c to (49)10c have their normal decimal values. And larger numbers are complemented by 100.

(50)10c = 50 − 100 = −50, (51)10c = 51 − 100 = −49, . . . , (98)10c = 98 − 100 = −2, (99)10c = 99 − 100 = −1

For instance (73)10c = 73 − 100 = −27 and (77)10c = 77 − 100 = −13.

• Three digits can represent [−500, 499]: For 3-digit numbers, (000)10c to (499)10c have their normal deci- mal values. And larger numbers are complemented by 1000.

(500)10c = 500 − 1000 = −500, . . . , (999)10c = 999 − 1000 = −1

For instance (073)10c = 73 and (873)10c = 873 − 1000 = −127. And so on. The advantage of this system is there is no need to build circuits for subtraction, addition will do. In- stances are:

(73)10c + (87)10c = (60)10c = −40 (in a 2-digit system)

(073)10c + (873)10c = (946)10c = −54 ((073)10c = 73, (873)10c = −127)

(999)10c + (873)10c = (872)10c = −128 (in a 3-digit system) A disadvantage of every finite number representation is overflow:

(373)10c + (227)10c = (600)10c = −400 (wrong in 3-digits)

(0373)10c + (0227)10c = (0600)10c = 600 (right in 4-digits)

(573)10c + (627)10c = (200)10c = 200 (wrong in 3-digits)

(9573)10c + (9627)10c = (9200)10c = −800 (right in 4-digits) The length of a ten’s complement number can be padded to make it longer.

• For positive numbers, pad with 0’s on the left: (473)10c = (0473)10c = (00473)10c = ···

• For negative numbers, pad with 9’s on the left: (773)10c = (9773)10c = (99773)10c = ···

Questions and Problems

1. What are the decimal values of the ten’s complement numbers:

(79)10c , (39)10c , (790)10c , (390)10c

2. What are the decimal values of sums:

(79)10c + (39)10c , (790)10c + (390)10c

3. What does it mean to say an operation is closed on a set? 4. There are at least two visualization of 10’s complement numbers. (a) On the number line from 0 to 99, pick up and move (shift, translate) the values 50 to 99 before 0.

This aligns (99)10c with −1 and (50)10c with −50. Draw this. (b) Wrap the number line from 0 to 99 around itself into a circle connecting 99 and 0. Draw this. 5. How would you define the 4’s complement number system? cse 1400/mth 2051 discrete math notes 8

Two’s Complement

Two’s complement is the number system use to represent positive and negative whole numbers: The integers Z = {0, ±1, ±2, . . .}, signed whole numbers.

• One bit can represent −1 or 0: (0)2c = 0 (1)2c = 1 − 2 = −1 • Two bits can represent {−2, −1, 0, 1}:

(00)2c = 0 (01)2c = 1

(10)2c = 2 − 4 = −2 (11)2c = 3 − 4 = −1

• Three bits can represent {−4, −3, −2, −1, 0, 1, 2, 3}:

(000)2c = 0 (001)2c = 1 (010)2c = 2 (011)2c = 3

(100)2c = 4 − 8 = −4 (101)2c = 5 − 8 = −3 (110)2c = 6 − 8 = −2 (111)2c = 7 − 8 = −1

And so on. The rules for two’s complement numbers are:

• A number (n)2c that begin with 0 (on the left) has value (n)2.

(0111)2c = 7, (01 0111)2c = 23, (010 0111)2c = 39

d • A number (n)2c that begin with 1 (on the left) has value (n)2 − 2 where d is the number of bits in n.

4 6 7 (1111)2c = 15 − 2 = −1, (11 0111)2c = 55 − 2 = −9, (110 0111)2c = 103 − 2 = −25

Like ten’s complement, the advantage of the two’s complement system is only addition is required: There is no need to build special circuits or code for subtraction. Instances are:

(0101)2c + (0001)2c = (0110)2c = 6 ((0101)2c = 5, (0001)2c = 1)

(1100)2c + (0011)2c = (1111)2c = −1 ((1100)2c = −4, (0011)2c = 3)

(1101)2c + (0111)2c = (0100)2c = 4 ((1101)2c = −3, (0111)2c = 7)

Overflow is still a disadvantage. It can be overcome by building larger registers, more bits per word. Or, eventually in software because any physical finite word length will overflow. Software will sometimes fail too: Somethings cannot be described finitely.

(1001)2c + (1101)2c = (0110)2c = 6 (wrong in a 4-bit system)

(1 1001)2c + (1 1101)2c = (1 0110)2c = −10 (right in a 5-bit system)

(0100)2c + (0101)2c = (1001)10c = −7 (wrong in a 4-bit system)

(0 0100)2c + (0 0101)2c = (0 1001)2c = 9 (right in a 5-bit system)

Overflow occurs when the last carry-in and carry-out bits do not match. cse 1400/mth 2051 discrete math notes 9

Two’s Complement: More Details

Pretend numbers are written using 8-bits:Not too short and not too long. Here are some facts about 8-bit two’s complement numbers.

1. Numbers that start with 0 are positive: For instance, (0111 1010)2c = 122 is positive. 2. Positive integers from 0 to 127 can be written with 8 bits.

3. Numbers that start with 1 are negative: For instance (1000 0110)2c = −122 is negative. 4. Negative integers from −128 = 128 − 256 to −1 = 255 − 256 can be written with 8 bits.

It is easy to negate two’s complement numbers. A number and its negative sum to 0. For 8-bit, 8 two’s complement numbers, the sum a + (−a) = 256 = 2 = (1 0000 0000)2c, which is 0 to 8-bits. For instance, Carries 1 1 1 1 1 1 0 0 0 w = 1 0 0 1 1 1 0 0 −w = 0 1 1 0 0 1 0 0 Sum 1 0 0 0 0 0 0 0 0 To keep this invariant, given a two’s complement number a you can find −a using two steps: 1. Copy a’s bits from right-to-left up to and including the first 1. 2. Flip the remaining bits to the left. For instance, the negative of

flip copy ! (1000 1000)2c is 0111 1000 . 2c

A ninth bit is needed to negate (1000 0000)2c = −128. The negative of

flip copy  −128 = (1 1000 0000)2c is 0 1000 0000 . 2c Some teach one’s complement and use one’s complement plus 1 to describe . I don’t do this, I encour- age you to learn about it on your own.

Questions and Problems

1. Write 256 as a power of 2, well really, write the value of all powers of 2 up to 210. 2. How many 4-bit numbers are there? Pick a few and think of them as two’s complement numbers. What are the decimal values of these bit patterns? 3. Write a few 8-bit two’s complement numbers. Write their negatives. Confirm that their 8-bit sums are all 0’s. 4. What is the range of 16-bit two’s complement numbers?