Professional Documents
Culture Documents
Pooja Vashisth
Pooja Vashisth
Pooja Vashisth
CSC 258
Week 5
Numerical Representation
Pooja Vashisth
1. Convert numerical data from one format to another
2. Interact with numbers in a fixed-width 2’s-complement
representation
3. Describe underflow, overflow, round off, and truncation errors
in data representations
4. Arithmetic operations on hardware
3 3
Numeric data formats
Binary Arithmetic and overflow
Negative integers' binary representation
How representations affect accuracy and precision
Investigate hardware implementation of various arithmetic
operations
Addition, subtraction, multiplication, division
4 4
CSC 258
5
x = x n−1 2n−1 + x n−2 2n−2 + + x1 21 + x 0 20
Range: 0 to +2n – 1
Example
0000 0000 … 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
Using 64 bits: 0 to
+18,446,774,073,709,551,615
6
Base 8
Compact representation of bit strings
3 bits per octal digit
0 000 4 100
1 001 5 101
2 010 6 110
3 011 7 111
Example: 7650
111 110 101 000
7
Base 16
Compact representation of bit strings
4 bits per hex digit
8
(Practice) Quiz time!
9
Quick reference:
10
Sign-magnitude notation is
the simplest method of
representing positive and
negative numbers.
Negative numbers have a
‘1’ as sign bit whereas
positive numbers have a
‘0’.
11
Sign-magnitude has some disadvantages.
Disadvantages:
We can have a positive result for zero, +0 or 00002, and a negative
result for zero, -0 or 10002. Both are valid.
Hence one of the bit patterns, e.g. 10002 is wasted.
Addition doesn't work the way we want it to. Try adding +1 and -
1: 00012 + 10012
12
x = − x n−1 2n−1 + x n−2 2n−2 + + x1 21 + x 0 20
13
The top bit is still a sign bit.
1 for negative numbers
0 for non-negative numbers
14
Negate and add 1
x + x = 1111...1112 = −1
x + 1 = −x
Example: representation of -2
+2 = 0000 0000 … 00102
–2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
15
Representing a number using more bits
Preserve the numeric value
17
CSC 258
18
Operations on integers
Addition and subtraction
Multiplication and division
Dealing with overflow
19
Example: 7 + 6
20
Multimedia extensions to standard instruction sets often offer
Saturating Arithmetic to deal with overflow.
21
Unsigned integers are commonly used for memory
addresses where overflows are ignored.
22
Arithmetic Adventure!
We are now going to learn to work out our solutions for
various binary arithmetic problems.
23
Sample Problem Solution
Consider an addition problem with two binary numbers, the sixth bit is used for sign,
and if the result exceeds +3110 or is less than -3210 , that causes an overflow.
Let’s try adding -1710 and -1910 to see how this overflow condition works for
excessive negative numbers:
The (incorrect) answer is a positive twenty-eight. The real sum of -1710 and -1910
has too large a magnitude to be properly represented with a five bit magnitude
field and a sixth sign bit. Applying saturated arithmetic the result is: -3210
24
Start with long-multiplication approach
multiplicand
1000
multiplier
× 1001
1000
0000
0000
1000
product 1001000
Length of product is
the sum of operand
lengths
25
Initially 0
26
A Multiplication Algorithm
Example
Using 4-bit numbers to save space, multiply 2ten × 3ten, or 0010two × 0011two.
Answer
Can be pipelined
Several multiplication performed in parallel
29
Four multiply instructions:
mul: multiply
Gives the lower 64 bits of the product
mulh: multiply high
Gives the upper 64 bits of the product, assuming the operands are
signed
mulhu: multiply high unsigned
Gives the upper 64 bits of the product, assuming the operands are
unsigned
mulhsu: multiply high signed/unsigned
Gives the upper 64 bits of the product, assuming one operand is
signed and the other unsigned
Use mulh result to check for 64-bit overflow
30
Check for 0 divisor
Long division approach
quotient
If divisor ≤ dividend bits
dividend 1 bit in quotient, subtract
1001 Otherwise
1000 1001010 0 bit in quotient, bring down next
-1000 dividend bit
divisor
10
101 Restoring division
1010 Do the subtract, and if remainder goes
-1000 < 0, add divisor back
remainder 10 Signed division
Divide using absolute values
n-bit operands yield n-bit
Adjust sign of quotient and remainder
quotient and remainder
as required
31
Initially divisor
in left half
Initially dividend
32
A Divide Algorithm
Example
Using a 4-bit version of the algorithm to save pages, let’s try
dividing 7ten by 2ten, or 0000 0111two by 0010two.
Answer
34
Can’t use parallel hardware as in multiplier
Subtraction is conditional on sign of remainder
35
Four instructions:
div, rem: signed divide, remainder
divu, remu: unsigned divide, remainder
36
CSC 258
37
This material gets into representation of real numbers, and you
should know these terms:
Overflow / Underflow
Precision / Accuracy
38
39
Assume a 32-bit format which reserves 1 bit for the sign, 15 bits
for the integer part and 16 bits for the fractional part.
Then, -43.625 is represented as following:
40
For 32-bit positive numbers:
What are the values of the smallest and the largest 32-bit positive
number for the above format?
41
The smallest positive number is:
2-16 ≈ 0.00001525878 (approximate)
42
The advantage of using a fixed-point
representation is performance. The hardware for
integers is sufficient.
The disadvantage is a relatively limited range of
values and precision of values that can be
represented.
Fixed point is usually inadequate for numerical analysis
as it does not allow enough numbers and accuracy.
Any number whose representation requires more than
the set number of bits allocated to the integer would
have to be stored inexactly.
We will learn more in the Gallery Walk
43
We will take a Gallery Walk to discuss fixed point applications in
software
Goal: Identify problems that may result due to accuracy issues
44
We’ve presented a fixed point representation of (some)
real numbers
45
There’s a shift in topics next week after.
We’re moving away from assembly and to
computer organization.
46
47
• Submit READY? Quizzes before classes
• Submit your group homework1 this week
• Participate in Peer discussion and Q/A every week
• Check your labs schedule… (Lab C)
• Study for the first term test …. All the Best!!!
48
• Submit Homework online this week (week 5)
• Do the practice questions for the week
#?: #3.1, 3.6, 3.11*, 3.13*
49
Floating point representation
Floating point arithmetic
50
51