Download CSE 20 * Discrete Mathematics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Georg Cantor's first set theory article wikipedia , lookup

Wiles's proof of Fermat's Last Theorem wikipedia , lookup

Large numbers wikipedia , lookup

Mathematical proof wikipedia , lookup

Collatz conjecture wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Approximations of Ο€ wikipedia , lookup

Arithmetic wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
CSE 20: Discrete Mathematics
for Computer Science
Prof. Shachar Lovett
2
Today’s Topics:
1.
2.
Number representations in different bases
Converting between bases
3
1. Number
representations
all your base are belong to us
Numbers are building blocks
5
Bases
ο‚› Base
𝑏β‰₯2
ο‚› Number
ο‚›
ο‚›
ο‚› In
in base b: dkdk-1…d1d0
Digits between 0 and b-1
Number is d0+d1*b+d2*b2+…+dk*bk
every base b, any integer has a unique
representation in this base
Values in different bases
What’s the decimal value of
(10001)2?
A.
B.
C.
D.
E.
(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?
A.
B.
C.
D.
E.
(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?
A.
B.
C.
D.
E.
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
A.
B.
C.
D.
E.
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.
10
Existence and uniqueness
ο‚› Theorem:
For any integer 𝑛 β‰₯ 0 and any
base 𝑏 β‰₯ 2, there is exactly one way to
write n in base b
ο‚› Need
ο‚› We
to prove existence and uniqueness
will prove for b=2; the proof can be
extended to any b
11
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)
12
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
13
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
14
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
A.
B.
C.
D.
E.
(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
A.
B.
C.
D.
E.
(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
End
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?
Other numbers?
ο‚› Fractional
components
ο‚› Negative
numbers aka
how to subtract
A.
B.
C.
… first, how do we add?
D.
E.
111
100
1011
1111
Other
One bit addition
Carry:
1
101
+110
1011
Subtraction
6
ο‚›
Borrowing
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 ]
JS p.
2’s complement
0000
1111
1110
1101
1100
-2
0
0010
1
2
-3
3
4
-4
-5
-6
Complete the
wheel of
numbers!
-1
0001
5
-7
6
-8
1001 1000
How many
numbers are we
representing with 4
bits?
7
0111
0011
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?
? ?
1
+1
? ?
?
1
0
?
?
0
1
?
?
0
1
?
?
1
0
?
?
0
1
?
?
1
1
?
? carry
0 1
0 1
? ?
How to add binary numbers?
? ?
1
+1
? ?
ο‚› Two
?
1
0
?
?
0
1
?
?
0
1
?
?
1
0
?
?
0
1
?
basic operations:
ο‚› One-Bit-Addition(bit1, bit2, carry)
ο‚› Next-carry(bit1, bit2, carry)
?
1
1
?
? carry
0 1
0 1
? ?
Numbers … logic … circuits