Lecture3
Lecture3
Announcement
• Programming Assignment 1 is out
• Details:
https://www.cs.rochester.edu/courses/252/fall2024/labs/
assignment1.html
• Due on Sep 16th, 11:59 PM
• You have 3 slip days
2
Carnegie Mellon
Announcement
• Programming Assignment 1 is in C language.
• Seek help from TAs.
• TAs are best positioned to answer your questions about
programming assignments!!!
• Programming assignments do NOT repeat the lecture
materials. They ask you to synthesize what you have
learned from the lectures and work out something new.
• Pay attention to Blackboard announcements
• There are changes about the office hour locations/time…
• I have to move my office hour tomorrow to early next
week.
3
Carnegie Mellon
Last Lecture
• Why Binary (bits)?
• Bit-level manipulations
• Integers
• Representation: unsigned and signed
• Conversion, casting
• Expanding, truncating
• Addition, negation, multiplication, shifting
• Summary
4
Carnegie Mellon
5
Carnegie Mellon
Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
5
Carnegie Mellon
Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
5
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
6
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
6
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
6
Carnegie Mellon
Two-Complement Implications
• Only 1 zero Signed Binary
6
Carnegie Mellon
7
Carnegie Mellon
7
Carnegie Mellon
7
Carnegie Mellon
8
Carnegie Mellon
8
Carnegie Mellon
8
Carnegie Mellon
(unsigned) char 1 1
(unsigned) short 2 2
(unsigned) int 4 4
(unsigned) long 4 8
9
Carnegie Mellon
• C Language
C Data Type 32-bit 64-bit •#include <limits.h>
(unsigned) char 1 1 •Declares constants, e.g.,
•ULONG_MAX
(unsigned) short 2 2
•LONG_MAX
(unsigned) int 4 4 •LONG_MIN
(unsigned) long 4 8 •Values platform specific
9
Carnegie Mellon
10
Carnegie Mellon
12
Carnegie Mellon
13
Carnegie Mellon
13
Carnegie Mellon
13
Carnegie Mellon
Signed Extension
• Task:
• Given w-bit signed integer x
• Convert it to (w+k)-bit integer with same value
14
Carnegie Mellon
Signed Extension
• Task:
• Given w-bit signed integer x
• Convert it to (w+k)-bit integer with same value
• Rule:
• Make k copies of sign bit:
• X ′ = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
k copies of MSB
14
Carnegie Mellon
Signed Extension
• Task:
• Given w-bit signed integer x
• Convert it to (w+k)-bit integer with same value
• Rule:
• Make k copies of sign bit:
• X ′ = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
k copies of MSB w
X •••
•••
X¢ ••• •••
k w 14
Carnegie Mellon
Another Problem
unsigned short x = 47981;
unsigned int ux = x;
15
Carnegie Mellon
k copies of 0 w
X •••
•••
X¢ 0 0
••• 0 0
•••
k w 16
Carnegie Mellon
17
Carnegie Mellon
17
Carnegie Mellon
18
Carnegie Mellon
Unsigned Addition
Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
19
Carnegie Mellon
Unsigned Addition
• Similar to Decimal Addition Unsigned Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
19
Carnegie Mellon
Unsigned Addition
• Similar to Decimal Addition Unsigned Binary
19
Carnegie Mellon
Unsigned Addition
• Similar to Decimal Addition Unsigned Binary
110 6
Overflow +) 101 +) 5
Case 1011 11
19
Carnegie Mellon
Unsigned Addition
• Similar to Decimal Addition Unsigned Binary
110 6
Overflow +) 101 +) 5
Case 1011 11 True Sum
19
Carnegie Mellon
Unsigned Addition
• Similar to Decimal Addition Unsigned Binary
110 6
Overflow +) 101 +) 5
Case 1011 11 True Sum
011 3 Sum with same bits
19
Carnegie Mellon
Unsigned Addition in C
Operands: w bits u •••
+v •••
True Sum: w+1 bits
u+v •••
Discard Carry: w bits UAddw(u , v) •••
20
Carnegie Mellon
21
Carnegie Mellon
21
Carnegie Mellon
21
Carnegie Mellon
110 -2
Overflow +) 101 +) -3
Case 1011 -5
21
Carnegie Mellon
110 -2
Overflow +) 101 +) -3
Case 1011 -5
011 3
21
Carnegie Mellon
110 -2
Overflow +) 101 +) -3
Case 1011 -5
011 3
Negative Overflow
21
Carnegie Mellon
110 -2 011 3
Overflow +) 101 +) -3 +) 001 +) 1
Case 1011 -5 0100 4
011 3
Negative Overflow
21
Carnegie Mellon
110 -2 011 3
Overflow +) 101 +) -3 +) 001 +) 1
Case 1011 -5 0100 4
011 3 100 -4
Negative Overflow
21
Carnegie Mellon
110 -2 011 3
Overflow +) 101 +) -3 +) 001 +) 1
Case 1011 -5 0100 4
011 3 100 -4
22
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
23
Carnegie Mellon
Multiplication
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
OMax 2w –1–1
OMin –2w –1
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
OMax 2w –1–1
0
0
OMin –2w –1
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
0
0
OMin –2w –1
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
0
0
OMin –2w –1
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
0
0
OMin –2w –1
PMin
24
Carnegie Mellon
Multiplication
• Goal: Computing Product of w-bit numbers x, y
0
0
OMin –2w –1
Multiplication
• Goal: Computing Product of w-bit numbers x, y
0
0
OMin –2w –1
Multiplication
• Goal: Computing Product of w-bit numbers x, y
• Exact results can be bigger than w bits
• Up to 2w bits (both signed and unsigned)
Original Number (w bits) Product (2w bits)
PMax 22w-2 OMin2
OMax 2w –1–1
0
0
OMin –2w –1
Unsigned Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u·v ••• •••
Discard w bits: w bits
•••
25
Carnegie Mellon
Unsigned Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u·v ••• •••
Discard w bits: w bits
•••
Signed Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u·v ••• •••
Discard w bits: w bits
•••
26
Carnegie Mellon
Signed Multiplication in C
u •••
Operands: w bits
* v •••
True Product: 2*w bits u·v ••• •••
Discard w bits: w bits
•••
• Examples
• u << 3 == u * 8
• (u << 5) – (u << 3) == u * 24
• Most machines shift and add faster than multiply
• Compiler generates this code automatically
Carnegie Mellon
28
Carnegie Mellon
• Multiplication:
• Unsigned/signed: Normal multiplication followed by truncate,
same operation on bit level
• Shift: Power-of-2 Multiply
Carnegie Mellon
33
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
34
Carnegie Mellon
= 2.2510
34
Carnegie Mellon
2i
2i-1
4
••• 2
1
2-j
35
Carnegie Mellon
36
Carnegie Mellon
Fixed-Point Representation
Decimal Binary
• Binary point stays fixed 0 00.00
• Fixed interval between two representable 0.25 00.01
numbers as long as the binary point stays fixed 0.5 00.10
0.75 00.11
• The interval in this example is 0.2510
1 01.00
• Fixed-point representation of numbers 1.25 01.01
• Integer is one special case of fixed-point 1.5 01.10
1.75 01.11
2 10.00
0 1 2 3 2.25 10.01
2.5 10.10
2.75 10.11
3 11.00
3.25 11.01
3.5 11.10
3.75 11.11
38
Carnegie Mellon
Fixed-Point Representation
Decimal Binary
• Binary point stays fixed 0 00.00
0000.
• Fixed interval between two representable 0.25
1 00.01
0001.
numbers as long as the binary point stays fixed 0.5
2 00.10
0010.
0.75
3 00.11
0011.
• The interval in this example is 0.2510
1
4 01.00
0100.
• Fixed-point representation of numbers 1.25
5 01.01
0101.
• Integer is one special case of fixed-point 1.5
6 01.10
0110.
1.75
7 01.11
0111.
2
8 10.00
1000.
0 1 2 3 4 5 6 7 …. 15 2.25
9 10.01
1001.
2.5
10 10.10
1010.
2.75
11 10.11
1011.
3
12 11.00
1100.
3.25
13 11.01
1101.
3.5
14 11.10
1110.
3.75
15 11.11
1111.
38
Carnegie
CarnegieMellon
Mello
39
Carnegie
CarnegieMellon
Mello
39
Carnegie
CarnegieMellon
Mello
b3b2.b1b0
0 1/4 1/2 3/4 5/4 3/2 7/4 2 …. 15/4
39
Carnegie
CarnegieMellon
Mello
b3b2.b1b0
0 1/4 1/2 3/4 5/4 3/2 7/4 2 …. 15/4
39
Carnegie
CarnegieMellon
Mello
b3b2.b1b0
0 1/4 1/2 3/4 5/4 3/2 7/4 2 …. 15/4
39
Carnegie Mellon
40
Carnegie Mellon
40
Carnegie Mellon
40
Carnegie Mellon
+∞
0 ….
40
Carnegie Mellon
+∞
0 ….
A Large
Number
40
Carnegie Mellon
Unrepresentable
small numbers
+∞
0 ….
A Large
Number
40
Carnegie Mellon
+∞
0
40
Carnegie Mellon
+∞
0
A Small
Number
40
Carnegie Mellon
Unrepresentable
large numbers
+∞
0
A Small
Number
40
Carnegie
CarnegieMellon
Mello
41
Carnegie Mellon
42
Carnegie Mellon
42
Carnegie Mellon
M × 10E
42
Carnegie Mellon
M × 10E
Significand
Decimal Value Scientific Notation
2 2×100
-4,321.768 -4.321768×103
0.000 000 007 51 7.51×10−9
42
Carnegie Mellon
M × 10E
Significand Base
Decimal Value Scientific Notation
2 2×100
-4,321.768 -4.321768×103
0.000 000 007 51 7.51×10−9
42
Carnegie Mellon
M × 10E Exponent
Significand Base
Decimal Value Scientific Notation
2 2×100
-4,321.768 -4.321768×103
0.000 000 007 51 7.51×10−9
42
Carnegie Mellon
43
Carnegie Mellon
44
Carnegie Mellon
45
Carnegie Mellon
45
Carnegie Mellon
s exp frac
45
Carnegie Mellon
s exp frac
45
Carnegie Mellon
s exp frac
45
Carnegie Mellon
s exp frac
45
Carnegie
CarnegieMellon
Mello
1 3 2
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
• bias is always 2k-1 - 1, where k is number of exponent bits
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
• bias is always 2k-1 - 1, where k is number of exponent bits
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
• bias is always 2k-1 - 1, where k is number of exponent bits
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value
• If exp were E, we could represent exponents from 0 to 7
• How about negative exponent?
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
• bias is always 2k-1 - 1, where k is number of exponent bits
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value E exp
• If exp were E, we could represent exponents from 0 to 7 -3 000
• How about negative exponent? -2 001
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
-1 010
0 011
• bias is always 2k-1 - 1, where k is number of exponent bits
1 100
• Example when we use 3 bits for exp (i.e., k = 3): 23 101
110
• bias = 3 4 111
• If E = -2, exp is 1 (0012)
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value E exp
• If exp were E, we could represent exponents from 0 to 7 -3 000
• How about negative exponent? -2 001
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
-1 010
0 011
• bias is always 2k-1 - 1, where k is number of exponent bits
1 100
• Example when we use 3 bits for exp (i.e., k = 3): 23 101
110
• bias = 3 4 111
• If E = -2, exp is 1 (0012)
• Reserve 000 and 111 for other purposes (more on this later)
46
Carnegie
CarnegieMellon
Mello
1 3 2
• exp has 3 bits, interpreted as an unsigned value E exp
• If exp were E, we could represent exponents from 0 to 7 -3 000
• How about negative exponent? -2 001
• Subtract a bias term: E = exp - bias (i.e., exp = E + bias)
-1 010
0 011
• bias is always 2k-1 - 1, where k is number of exponent bits
1 100
• Example when we use 3 bits for exp (i.e., k = 3): 23 101
110
• bias = 3 4 111
• If E = -2, exp is 1 (0012)
• Reserve 000 and 111 for other purposes (more on this later)
• We can now represent exponents from -2 (exp 001) to 3 (exp 110)
46
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M
• frac = 10 implies M = 1.10
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M
• frac = 10 implies M = 1.10
• Putting it Together: An Example:
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M
• frac = 10 implies M = 1.10
• Putting it Together: An Example:
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M
• frac = 10 implies M = 1.10
• Putting it Together: An Example:
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M
• frac = 10 implies M = 1.10
• Putting it Together: An Example:
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M E exp
• frac = 10 implies M = 1.10 -3 000
• Putting it Together: An Example: -2 001
-1 010
0 011
1 100
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M E exp
• frac = 10 implies M = 1.10 -3 000
• Putting it Together: An Example: -2 001
-1 010
0 011
1 100
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M E exp
• frac = 10 implies M = 1.10 -3 000
• Putting it Together: An Example: -2 001
-1 010
0 011
1 100
47
Carnegie
CarnegieMellon
Mello
1 3 2
• frac has 2 bits, append them after “1.” to form M E exp
• frac = 10 implies M = 1.10 -3 000
• Putting it Together: An Example: -2 001
-1 010
0 011
1 100
47