CSE111: Great Ideas in Computer Science
Dr. Carl Alphonce219 Bell Hall
Office hours: M-F 11:00-11:50
Announcements
• No recitations this week. First meeting of recitations in week of 1/25-1/29.
• Extra copies of syllabus available at course web-site (address is on UB Learns).
2
cell phones off(please)
3
Agenda
• Review from last class– binary numbers– binary arithmetic
• Today’s topics– fixed-width representations– two’s complement
4
5
Review
• Binary numbers– Digits are ‘0’ and ‘1’– Weight of positions are powers of two
• Binary arithmetic– Same procedure as for base 10
6
Counting
Decimal (base 10)
0123456789
10111213
etc.
Binary (base 2)
01
1011
100101110111
100010011010101111001101
etc.
7
Number systems
Decimal (base 10)• Each position is weighted by
a power of 10.• E.g. 734 =
– 7*100 + 3*10 + 4*1– 7*102 + 3*101 + 4*100
• E.g. 1101 = – 1*1000 + 1*100 + 0*10 + 1*1– 1*103 + 1*102 + 0*101 + 1*100
Binary (base 2)• Each position is weighted by a
power of 2.• E.g. 111 =
– 1*4 + 1*2 + 1*1 = “seven”– 1*22 + 1*21 + 1*20
• E.g. 1101 = – 1*8 + 1*4 + 0*2 + 1*1 = “thirteen”– 1*23 + 1*22 + 0*21 + 1*20
8
Binary Arithmetic
• Operations in base 2 work the same as in base 10.
• Addition: 2 + 3 = 510
+11101
9
Exercises
• Compute the following sums, in base 2
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
10
Setting up the exercises
• Compute the following sums, in base 2
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
101 1000 1010+ 001 + 1000 + 1100
11
Solving up the exercises
• Compute the following sums, in base 2
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
101 1000 1010+ 001 + 1000 + 1100 110 10000 10110
12
Fixed-width encodings
• Suppose we have a four-bit wide representation.• We then have 24 = 2*2*2*2 = 16 distinct bit
patterns:0000 10000001 10010010 10100011 10110100 11000101 11010110 11100111 1111
13
Exercises
• Compute the following sums, in a four-bit wide base 2 representation:
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
14
Setting up the exercises
• Compute the following sums, in a four-bit wide base 2 representation:
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
101 1000 1010+ 001 + 1000 + 1100
15
Solving the exercises
• Compute the following sums, in a four-bit wide base 2 representation:
5 + 1 = 6 8 + 8 = 16 10 + 12 = 22
101 1000 1010+ 001 + 1000 + 1100 110 0000 0110
16
What can they represent?
• Up to sixteen distinct things:– E.g. the numbers 0 through 15, or 1
through 16, or 10 through 25, or …– (notice the limited range!)
– Types of animals (aardvark, bat, cat, dog, …, octopus, penguin)
– Anything else we want!
17
How about negative and non-negative numbers?
• How can we assign the bit strings from 0000 to 1111 to numbers, including negative numbers?
• There are many ways to do this, but some ways are better than others.
• Keep in mind, we only have the symbols ‘0’ and ‘1’ to use.
18
Desirable properties of our
representationX – Y = X + (-Y)
X + (-X) = 0
0 = - 0
- (-X) = X
19
Two’s complement
0000 0 1000 -80001 1 1001 -70010 2 1010 -60011 3 1011 -50100 4 1100 -40101 5 1101 -30110 6 1110 -20111 7 1111 -1
20
How does it work?
• Given representation for x, compute representation of –x as follows:– First compute the 1’s complement– Then compute the 2’s complement
21
How does it work?
• Given representation for x, compute representation of –x as follows:– First compute the 1’s complement by
inverting all the bits (change 0 to 1, and 1 to 0)
– Then compute the 2’s complement by adding 1, and ignoring any overflow carry bit.
22
Examples
• Compute the representation of -1:
0001 original representation1110 one’s complement1111 two’s complement
• Representation of -1 is 1111.
23
Check: x + (–x) = 0
0001+ 1111 0000
• Remember, representation is 4 bits wide, and we discard any overflow carry bit.
24
Check other properties too!(done on board)
X – Y = X + (-Y)
X + (-X) = 0
0 = - 0
- (-X) = X
25
Questions?