Computer Arithmetic Representations
Computer Arithmetic Representations
Representations
• Representation of data
2
Arithmetic
• Operations on integers
• Addition and Subtraction
• ALU: arithmetic and logic unit
• Multiply
• Divide
• Floating Point real numbers
• notation
• add
• multiply
3
Number Representations
• Decimal number system:
4382 = 4x103 + 3x102 + 8x101 + 2x100
• Binary numbers (base 2) a0 : least significant bit (lsb)
an-1: most significant bit (msb)
(an-1 an-2... a1 a0) two = an-1 x 2n-1 + an-2 x 2n-2 + … + a0 x 20
n : Position
a : Digit
• With n bits 2n possible combinations 2 : Base (radix/weight)
1 bit 2 bits 3 bits 4 bits decimal
0 00 000 0000 0
1 01 001 0001 1
10 010 0010 2
11 011 0011 3 How do we represent
100 0100 4 negative numbers????
101 0101 5
110 0110 6
111 0111 7
1000 8
1001 9
4
Unsigned Integer
• Consider a n-bit vector of the form:
A 2 n 1 an 1 2 n 2 an 2 21 a1 20 a0
Signed Integer
• 3 major representations:
Sign and magnitude
One’s complement
Two’s complement
• Assumptions:
4-bit machine word
16 different values can be represented
Roughly half are positive, half are negative
Signed binary numbers
Possible representations:
-3 10 11 01 00 +4 1 100 = - 4
10 10 01 01
-2 +5 -
10 01 01 10
-1 10 00 01 11 +6
-0 +7
-0 +0
-1 11 11 00 00 +1
11 10 00 01
-2 +2 +
11 01 00 10
-3 11 00 00 11 +3 0 100 = + 4
-4 10 11 01 00 +4 1 011 = - 4
10 10 01 01
-5 +5 -
10 01 01 10
-6 10 00 01 11 +6
-7 +7
-1 +0
like 1's comp except shifted
one position clockwise -2 11 11 00 00 +1
11 10 00 01
-3 +2 +
11 01 00 10
-4 11 00 00 11 +3 0 100 = + 4
-5 10 11 01 00 +4 1 100 = - 4
10 10 01 01
-6 +5 -
10 01 01 10
-7 10 00 01 11 +6
-8 +7
B Values represented
Sign and
b 3 b 2 b1 b 0 magnitude 1's complement 2's complement
0 1 1 1 +7 +7 + 7
0 1 1 0 +6 +6 + 6
0 1 0 1 +5 +5 + 5
0 1 0 0 +4 +4 + 4
0 0 1 1 +3 +3 + 3
0 0 1 0 +2 +2 + 2
0 0 0 1 +1 +1 + 1
0 0 0 0 +0 +0 + 0
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7
1 0 1 0 - 2 -5 - 6
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4
1 1 0 1 - 5 -2 - 3
1 1 1 0 - 6 - 1 - 2
1 1 1 1 - 7 -0 - 1
Comparison
• Sign and Magnitude
– Cumbersome addition/subtraction
– Must compare magnitudes to determine sign of result
• One’s Complement
– Simply bit-wise complement
• Two’s Complement
– Simply bit-wise complement + 1
Floating Point
• We need a way to represent
– numbers with fractions, e.g., 3.1416
– very small numbers, e.g., .000000001
– very large numbers, e.g., 3.15576 109
• Representation:
– sign, exponent, significand:
(–1)sign significand 2exponent
– more bits for significand gives more accuracy
– more bits for exponent increases range
13
IEEE 754 floating point standard
Has greatly improved portability of scientific applications
S E M
S E M
1 bit 11 bits 52 bits
FP numbers are in signed magnitude representation of the form
(-1)S x M x BE
where
S is the sign bit (0=positive, 1=negative)
M is the mantissa (also called the significand)
B is the base (implied)
E is the exponent
Normalizing means
• Shifting the decimal point until we have the right number of digits to its left
(normally one) [i.e.,Only a single non-zero digit before the radix point]
• Adding or subtracting from the exponent to reflect the shift
Normalizing Procedure
• Exponent could be positive or negative, hence Add a bias to exponent to make it strictly
positive.
– Bias of 127 for single precision and 1023 for double precision
Example:
– binary : - 0.11 = -1.1 x 2-1
– floating point: exponent = -1+bias = -1+127=126 = 011111102
– IEEE single precision:
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0000000000
18
Unsigned Integer (E′)
E′=E+Bias
E′=E+127
E′=E+1023
IEEE 754 Floating Point standard Formats
IEEE 754 floating point Normalization
It requires an exponent less than -126 (underflow) or greater than +127 (overflow).
Both are exceptions that need to be considered.
Special Values
• The end value 0 and 255 are used to represent special values.
• When E’=0 and M≠0, denormal numbers are represented. The value is
±0.M2-126.