03 Integersfloats
03 Integersfloats
Computer
system:
Integers
¢ Representation of integers: unsigned and signed
¢ Casting
¢ Arithmetic and shifting
¢ Sign extension
¢ 4 bits for suit, 13 bits for card value – 17 bits with two set to 1
suit value
VALUE_MASK = 0x0F = 0 0 0 0 1 1 1 1
suit value
Encoding Integers
¢ The hardware (and C) supports two flavors of integers:
§ unsigned – only the non-negatives
§ signed – both negatives and non-negatives
0110010110101001
Autumn 2013 Integers & Floats 10
University of Washington
Unsigned Integers
¢ Unsigned values are just what you expect
§ b7b6b5b4b3b2b1b0 = b727 + b626 + b525 + … + b121 + b020
§ Useful formula: 1+2+4+8+...+2N-1 = 2N - 1
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
–0 +7
Autumn 2013 Integers & Floats 13
University of Washington
Sign-and-Magnitude Negatives
¢ How should we represent -1 in binary?
§ 100000012
Use the MSB for + or -, and the other bits to give magnitude.
(Unfortunate side effect: there are two representations of 0!)
–7 +0
–6 1111 0000 +1
–5 1110 0001 +2
1101 0010
–4 +3
1100 0011
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
–0 +7
Autumn 2013 Integers & Floats 14
University of Washington
Sign-and-Magnitude Negatives
¢ How should we represent -1 in binary?
§ 100000012
Use the MSB for + or -, and the other bits to give magnitude.
(Unfortunate side effect: there are two representations of 0!)
§ Another problem: arithmetic is cumbersome.
§ Example: –7 +0
4 - 3 != 4 + (-3) –6 1111 0000 +1
–5 1110 0001 +2
1101 0010
0100 –4 +3
+1011 1100 0011
1111
– 3 1011 0100 + 4
1010 0101
–2 1001 0110 +5
–1 1000 0111 +6
How do we solve these problems? –0 +7
Autumn 2013 Integers & Floats 15
University of Washington
–1 0
–2 1111 0000 +1
–3 1110 0001 +2
1101 0010
–4 +3
1100 0011
– 5 1011 0100 + 4
1010 0101
–6 1001 0110 +5
–7 1000 0111 +6
–8 +7
–1 0
–2 1111 0000 +1
–3 1110 0001 +2
1101 0010
–4 +3
1100 0011
– 5 1011 0100 + 4
1010 0101
–6 1001 0110 +5
–7 1000 0111 +6
Autumn 2013 Integers & Floats –8 +7 17
University of Washington
– 5 1011 0100 + 4
1010 0101
–6 1001 0110 +5
–7 1000 0111 +6
Autumn 2013 Integers & Floats –8 +7 18
University of Washington
23 x 1 + 22 x 0 + 21 x 1 + 20 x 1 -23 x 1 + 22 x 0 + 21 x 1 + 20 x 1
15 0 –1 0
14 1111 0000 1 –2 1111 0000 +1
23 x 1 + 22 x 0 + 21 x 1 + 20 x 1 -23 x 1 + 22 x 0 + 21 x 1 + 20 x 1
11 -5
(math) difference = 16 = 24
15 0 –1 0
14 1111 0000 1 –2 1111 0000 +1
23 x 1 + 22 x 0 + 21 x 1 + 20 x 1 -23 x 1 + 22 x 0 + 21 x 1 + 20 x 1
11 -5
(math) difference = 16 = 24
15 0 –1 0
14 1111 0000 1 –2 1111 0000 +1
Two’s Complement
¢ Why does it work?
§ Put another way, for all positive integers x, we want:
§ bits( x ) + bits( –x ) = 0 (ignoring the carry-out bit)
00000010 00000011
+???????? +????????
00000000 00000000
Two’s Complement
¢ Why does it work?
§ Put another way, for all positive integers x, we want:
§ bits( x ) + bits( –x ) = 0 (ignoring the carry-out bit)
00000010 00000011
+???????? +????????
00000000 00000000
Two’s Complement
¢ Why does it work?
§ Put another way, for all positive integers x, we want:
§ bits( x ) + bits( –x ) = 0 (ignoring the carry-out bit)
00000010 00000011
+11111110 +11111101
100000000 100000000
Conversion Visualized
¢ Two’s Complement ® Unsigned
UMax
§ Ordering Inversion
UMax – 1
§ Negative ® Big Positive
TMax + 1 Unsigned
TMax TMax Range
2’s Complement 0 0
Range –1
–2
TMin
Autumn 2013 Integers & Floats 28
University of Washington
Overflow/Wrapping: Unsigned
addition: drop the carry bit
15 1111 15 0
+2 + 0010 13
14
1110
1111 0000
0001
1
2
17 10001 12
1101 0010
3
1100 0011
1 11 1011 0100 4
1010 0101
10 1001 0110 5
9 1000 0111 6
8 7
Modular Arithmetic
Autumn 2013 Integers & Floats 29
University of Washington
-1 1111 –1 0
–2 +1
+2 + 0010 –3 1110
1111 0000
0001 +2
1 10001 –4
1101 0010
+3
1100 0011
– 5 1011 0100 + 4
1010 0101
6 0110 –6 1001 0110 +5
+3 + 0011 –7
–8
1000 0111
+7
+6
9 1001
-7
Modular Arithmetic
Autumn 2013 Integers & Floats 30
University of Washington
Values To Remember
¢ Unsigned Values ¢ Two’s Complement Values
§ UMin = 0 § TMin = –2w–1
§ 000…0 § 100…0
§ UMax = 2w – 1 § TMax = 2w–1 – 1
§ 111…1 § 011…1
§ Negative one
§ 111…1 0xF...F
Values for W = 32
Decimal Hex Binary
UMax 4,294,967,296 FF FF FF FF 11111111 11111111 11111111 11111111
¢
Casting Surprises
Expression Evaluation
!!!
§ If you mix unsigned and signed in a single expression, then
signed values are implicitly cast to unsigned.
§ Including comparison operations <, >, ==, <=, >=
§ Examples for W = 32: TMIN = -2,147,483,648 TMAX = 2,147,483,647
¢ Constant1 Constant2 Relation Evaluation
00 0U0U == unsigned
-1-1 00 < signed
-1-1 0U0U > unsigned
2147483647
2147483647 -2147483648
-2147483648 > signed
2147483647U
2147483647U -2147483648
-2147483648 < unsigned
-1-1 -2-2 > signed
(unsigned)-1
(unsigned) -1 -2-2 > unsigned
2147483647
2147483647 2147483648U
2147483648U < unsigned
2147483647
2147483647 (int)
(int)2147483648U
2147483648U > signed
Autumn 2013 Integers & Floats 35
University of Washington
Sign Extension
¢ What happens if you convert a 32-bit signed integer to a 64-
bit signed integer?
Sign 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
Autumn 2013 Integers & Floats 37
University of Washington
8-bit representations
00001001 10000001
11111111 00100111
C: casting between unsigned and signed just reinterprets the same bits.
Autumn 2013 Integers & Floats 38
University of Washington
Sign Extension
0010 4-bit 2
00000010 8-bit 2
1100 4-bit -4
????1100 8-bit -4
Sign Extension
0010 4-bit 2
00000010 8-bit 2
1100 4-bit -4
00001100 8-bit 12
Sign Extension
0010 4-bit 2
00000010 8-bit 2
1100 4-bit -4
Sign Extension
0010 4-bit 2
00000010 8-bit 2
1100 4-bit -4
11111100 8-bit -4
Shift Operations
¢ Left shift: x << y Argument x 01100010
§ Shift bit vector x left by y positions
<< 3 00010000
§ Throw away extra bits on left
§ Fill with 0s on right Logical >> 2 00011000
¢ Right shift: x >> y Arithmetic >> 2 00011000
§ Shift bit-vector x right by y positions
§Throw away extra bits on right
§ Logical shift (for unsigned values) Argument x 10100010
§ Fill with 0s on left << 3 00010000
§ Arithmetic shift (for signed values)
Logical >> 2 00101000
§ Replicate most significant bit on left
§ Maintains sign of x Arithmetic >> 2 11101000
The behavior of >> in C depends on the compiler! It is arithmetic shift right in GCC.
Java: >>> is logical shift right; >> is arithmetic shift right.
Autumn 2013 Integers & Floats 44
University of Washington
Shift Operations
¢ Left shift: x << y Argument x 01100010
§ Shift bit vector x left by y positions
<< 3 00010000
§ Throw away extra bits on left
§ Fill with 0s on right Logical >> 2 00011000
¢ Right shift: x >> y Arithmetic >> 2 00011000
§ Shift bit-vector x right by y positions
§Throw away extra bits on right
§ Logical shift (for unsigned values) Argument x 10100010
§ Fill with 0s on left << 3 00010000
§ Arithmetic shift (for signed values)
Logical >> 2 00101000
§ Replicate most significant bit on left
§ Maintains sign of x Arithmetic >> 2 11101000
§ Why is this useful?
x >> 9?
The behavior of >> in C depends on the compiler! It is arithmetic shift right in GCC.
Java: >>> is logical shift right; >> is arithmetic shift right.
Autumn 2013 Integers & Floats 45
University of Washington
¢ x << m?
¢ x << m: multiply by 2m
y == 108
0001101100 shift in zeros from the right
y == 108
1001101100 shift in zeros from the right
Autumn 2013
clarification from Mon.: shifts byIntegers
n < &0Floats
or n >= word size are undefined 49
University of Washington
Multiplication
¢ What do you get when you multiply 9 x 9?
¢ 230 x 5?
¢ -231 x -231?
Unsigned Multiplication in C
void getstuff() {
char mybuf[MSIZE];
copy_from_kernel(mybuf, MSIZE);
printf(“%s\n”, mybuf);
}
void getstuff() {
char mybuf[MSIZE];
copy_from_kernel(mybuf, -MSIZE);
. . .
}
4
••• 2
.
1
bi bi–1 • • • b2 b1 b0 b–1 b–2 b–3 ••• b–j
1/2
1/4 •••
1/8
2–j
¢ Representation
§ Bits to right of “binary point” represent fractional powers of 2
§ Represents rational number: i
k
å bk ×2
k =- j
Autumn 2013 Integers & Floats 59
University of Washington
¢ Observations
§ Shift left = multiply by power of 2
§ Shift right = divide by power of 2
§ Numbers of the form 0.111111…2 are just below 1.0
¢ Limitations:
§ Exact representation possible only for numbers of the form x * 2y
§ Other rational numbers have repeating bit representations
§ 1/3 = 0.333333…10 = 0.01010101[01]…2
Autumn 2013 Integers & Floats 60
University of Washington
¢ Representation in memory:
§ MSB s is sign bit s
§ exp field encodes E (but is not equal to E)
§ frac field encodes M (but is not equal to M)
s exp frac
Precisions
¢ Single precision: 32 bits
s exp frac
1 bit 8 bits 23 bits
Autumn 2013
§ note: exp=11…1 and exp=00…0 are reserved, limiting exp range…
Integers & Floats 67
University of Washington
¢ x +f y = Round(x + y)
¢ x *f y = Round(x * y)
¢ Fixing
§ If M ≥ 2, shift M right, increment E
§ If E out of range, overflow
§ Round M to fit frac precision
¢ Fixing
§ If M ≥ 2, shift M right, increment E
§ if M < 1, shift M left k positions, decrement E by k
§ Overflow if E out of range
§ Round M to fit frac precision
Autumn 2013 Integers & Floats 70
University of Washington
Rounding modes
¢ Possible rounding modes (illustrate with dollar rounding):
$1.40 $1.60 $1.50 $2.50 –$1.50
§ Round-toward-zero $1 $1 $1 $2 –$1
§ Round-down (-¥) $1 $1 $1 $2 –$2
§ Round-up (+¥) $2 $2 $2 $3 –$1
§ Round-to-nearest $1 $2 ?? ?? ??
§ Round-to-even $1 $2 $2 $2 –$2
¢ Round-to-even avoids statistical bias in repeated rounding.
§ Rounds up about half the time, down about half the time.
§ Default rounding mode for IEEE floating-point
¢ Floats with value +¥, -¥, and NaN can be used in operations
§ Result usually still +¥, -¥, or NaN; sometimes intuitive, sometimes not
float f1 = 1.0;
float f2 = 0.0;
int i;
for ( i=0; i<10; i++ ) {
f2 += 1.0/10.0;
}
return 0;
}
Saved State 4
d7 … d4 0100 0000 0000 1001 0001 1110 1011 1000 3
Location
d3 … d0 0101 0000 … 2
accessed
a[1] 1 by fun(i)
a[0] 0
Autumn 2013 Integers & Floats 79
University of Washington
Saved State 4
d7 … d4 0100 0000 0000 1001 0001 1110 1011 1000 3
Location
d3 … d0 0100 0000 0000 0000 0000 0000 0000 0000 2
accessed
a[1] 1 by fun(i)
a[0] 0
Autumn 2013 Integers & Floats 80
University of Washington
Saved State 4
d7 … d4 0100 0000 0000 0000 0000 0000 0000 0000 3
Location
d3 … d0 0101 0000 … 2
accessed
a[1] 1 by fun(i)
a[0] 0
Autumn 2013 Integers & Floats 81
University of Washington
Summary
¢ As with integers, floats suffer from the fixed number of bits
available to represent them
§ Can get overflow/underflow, just like ints
§ Some “simple fractions” have no exact representation (e.g., 0.2)
§ Can also lose precision, unlike ints
§ “Every operation gets a slightly wrong result”
Normalized Values
s
V = (–1) * M * 2
E
s exp frac
k n
¢ Condition: exp ¹ 000…0 and exp ¹ 111…1
¢ Exponent coded as biased value: E = exp - Bias
§ exp is an unsigned value ranging from 1 to 2k-2 (k == # bits in exp)
§ Bias = 2k-1 - 1
Single precision: 127 (so exp: 1…254, E: -126…127)
§
§ Double precision: 1023 (so exp: 1…2046, E: -1022…1023)
§ These enable negative values for E, for representing very small values
¢ Result:
0 10001100 10000001110010000000000
s exp frac
Denormalized Values
¢ Condition: exp = 000…0
Special Values
¢ Condition: exp = 111…1
-¥ +¥
-Normalized -Denorm +Denorm +Normalized
NaN NaN
-0 +0
Distribution of Values
§ Bias is 23-1-1 = 3
-15 -10 -5 0 5 10 15
Denormalized Normalized Infinity
-1 -0.5 0 0.5 1
Denormalized Normalized Infinity
¢ Fixing
§ If M ≥ 2, shift M right, increment E
§ If E out of range, overflow
§ Round M to fit frac precision
E1–E2
¢ Exact Result: (–1)s M 2E
(–1)s1 M1
§ Sign s, significand M:
Result of signed align & add
§ + (–1)s2 M2
§ Exponent E: E1
(–1)s M
¢ Fixing
§ If M ≥ 2, shift M right, increment E
§ if M < 1, shift M left k positions, decrement E by k
§ Overflow if E out of range
§ Round M to fit frac precision
¢ Examples
§ Round to nearest 1/4 (2 bits right of binary point)
Value Binary Rounded Action Rounded Value
2 3/32 10.000112 10.002 (<1/2—down) 2
2 3/16 10.001102 10.012 (>1/2—up) 2 1/4
2 7/8 10.111002 11.002 ( 1/2—up) 3
2 5/8 10.101002 10.102 ( 1/2—down) 2 1/2