CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
Today’s Topics: Number representations in different bases Converting between bases
1. Number representations all your base are belong to us
Numbers are building blocks Five
Bases Base 𝑏≥2 Number in base b: dkdk-1…d1d0 Digits between 0 and b-1 Number is d0+d1*b+d2*b2+…+dk*bk In every base b, any integer has a unique representation in this base
Values in different bases What’s the decimal value of (10001)2? (5)10 (17)10 (-1)10 (10001)10 None of the above / more than one of the above.
Values in different bases What’s the base 2 representation of the decimal number (42)10? (111111)2 (100001)2 (101010)2 (110011)2 None of the above / more than one of the above.
Values in different bases What’s the biggest integer value whose binary representation has 4 bits? 24 = (16)10 23 = (8)10 (4)10 (1000)10 None of the above / more than one of the above.
Uniqueness Is it possible to have two different representations for an integer in base 2? That is, is it possible to have No. Yes, but m has to be the same as n. Yes, and m,n can be different but for each kind of coefficient that appears in both, it has to agree. That is, a0 = b0, a1 = b1, etc. Yes, if m=n and all the coefficients agree. More than one of the above / none of the above.
Existence and uniqueness Theorem: For any integer 𝑛≥0 and any base 𝑏≥2, there is exactly one way to write n in base b Need to prove existence and uniqueness We will prove for b=2; the proof can be extended to any b
Proof of existence (b=2) Proof by strong induction: Base: n=0 = (0)2 Inductive: assume for all k<n, prove for n Case 1: n is even, n=2k Assume k=(dm … d0)2 Then n=(dm … d0 0)2 Why? Adding a 2 to the right multiplies the number by 2 (shifts all digits by 1)
Proof of existence (b=2) Case 2: n is odd, n=2k+1 Assume k=(dm … d0)2 Then n=(dm … d0 1)2 Why? We already proved that (dm … d0 0)2=2k add 1 to both sides
Proof of uniqueness (b=2) Assume n=(dm…d0)2 = (et…e0)2 Want to prove: must be the same Need to assume that the most significant digits are nonzero, which in binary means they are 1 So assume dm=et=1 We will prove m=t and di=ei for all i
Proof of uniqueness (b=2) Proof by strong induction Base: n=0, only way is (0)2 Inductive: assume for all k<n, prove for n We know least significant digit is n MOD 2, so d0=e0=(n MOD 2) We “peel” the least digit: n DIV 2 = (dm…d1)2 = (et…e1)2 By strong induction, must be the same
Parity and shift
Shifts
Values in different bases What’s the base 2 representation of the decimal number (2014)10 (11111011110)2 (10000000000)2 (10101010101)2 (1000000001)2 None of the above / more than one of the above.
Values in different bases What’s the base 2 representation of the decimal number (2014)10 (11111011110)2 (10000000000)2 (10101010101)2 (1000000001)2 None of the above / more than one of the above. Is there a systematic way (aka algorithm) to do it?
Decimal to Binary conversion toBinary(pos int n) Begin binary=“” i=n While i>0 Do If (i is even) Then binary=“0”+binary End If (i is odd) Then binary=“1”+binary i=i DIV 2 Output binary End. Right to left Questions to ask: Does it always terminate? Does it give the correct answer? What is the time complexity? Biggest power of 2 less than. Recursive. Connect back to RPM.
Other numbers? Fractional components Negative numbers aka how to subtract … first, how do we add? 111 100 1011 1111 Other
One bit addition 1 Carry: 1 0 1 + 1 1 0 1 0 1 1
Subtraction JS p. 6 Borrowing Carrying Complementation A – B = (A – 10) + (10 – B) Carrying A – B = (A+10) – (B+10) Complementation A – B = A + Bc = A + [ (99-B) - 99 ] = A + [ (100-B) – 100 ]
2’s complement How many numbers are we representing with 4 bits? 0000 1111 0001 1110 -1 1 0010 -2 2 1101 0011 -3 3 1100 -4 4 5 -5 -6 6 -7 Complete the wheel of numbers! 7 -8 1001 0111 1000
How to add binary numbers? 1 1 0 0 1 0 1 0 1 +1 0 1 1 0 1 1 0 1 ? ? ? ? ? ? ? ? ? ?
How to add binary numbers? ? ? ? ? ? ? ? ? ? carry 1 1 0 0 1 0 1 0 1 +1 0 1 1 0 1 1 0 1 ? ? ? ? ? ? ? ? ? ?
How to add binary numbers? ? ? ? ? ? ? ? ? ? carry 1 1 0 0 1 0 1 0 1 +1 0 1 1 0 1 1 0 1 ? ? ? ? ? ? ? ? ? ? Two basic operations: One-Bit-Addition(bit1, bit2, carry) Next-carry(bit1, bit2, carry)
Numbers … logic … circuits