Integer Representation But first, encode deck of cards. Two possible

9/8/15
WELLESLEY CS 240
Integer Representation
But first, encode d eck o f cards.
Representation of integers: unsigned and signed
Sign extension
52 cards in 4 s uits
Arithmetic and shifting
Casting
What operations should be e asy to implement?
How do w e encode suits, face cards?
Get and compare rank
Get and compare suit
2
Two p ossible representations
Two b etter representations
Binary encoding of all 52 cards – only 6 bits needed
52 cards – 52 bits w ith bit corresponding to card set to 1
Number each card
Fits in one byte
low-­‐o rder 6 bits of a byte
Smaller t han one-­‐hot encodings.
How can w e make v alue and suit comparisons easier?
52 bits in 2 x 32-­‐b it words
“One-­‐hot” encoding
Two 32-­‐bit w ords
Hard t o compare v alues and suits
Large number of bits required
Binary encoding of s uit (2 bits) and value (4 bits) s eparately
Number each suit
Number each v alue
Fits in one byte
Easy suit, v alue comparisons
4 bits for suit, 1 3 bits for card value – 17 bits w ith two s et to 1
Pair of one-­‐hot encoded values
Fits in one 32-­‐bit w ord
Easier t o compare suits and v alues
Still space-­‐inefficient
3
4
suit
value
5
1
9/8/15
WELLESLEY CS 240
Compare Card Suits
mask: a bit vector that, when bitwise ANDed with another bit vector v, turns all but the bits of interest in v to 0
Compare Card Values
mask: a bit vector that, when bitwise ANDed with another bit vector v, turns all but the bits of interest in v to 0
static final SUIT_MASK = 0x30;
static final VALUE_MASK = 0x0F;
boolean sameSuit(char card1, char card2) {
return 0 == ((card1 & SUIT_MASK) ^ (card2 & SUIT_MASK));
// return (card1 & SUIT_MASK) == (card2 & SUIT_MASK);
}
boolean greaterValue(char card1, char card2) {
return (card1 & VALUE_MASK) > (card2 & VALUE_MASK));
}
SUIT_MASK = 0x30 = 0 0 1 1 0 0 0 0
suit
works even if value is stored in high b its
VALUE_MASK = 0x0F = 0 0 0 0 1 1 1 1
equivalent
value
suit
char hand[5];
// represents a 5-card hand
char card1, card2; // two cards to compare
card1 = hand[0];
card2 = hand[1];
...
if ( sameSuit(card1, card2) ) { ... }
value
char hand[5];
// represents a 5-card hand
char card1, card2; // two cards to compare
card1 = hand[0];
card2 = hand[1];
...
if ( greaterValue(card1, card2) ) { ... }
6
Encoding Integers in a fixed number of bits
Unsigned modular arithmetic, o verflow
Positional representation, fixed # of positions.
Two flavors:
unsigned (⊂ ) – non-­‐negatives only
signed (⊂ ) – both negatives and non-­‐negatives
fixed-­‐width representations: W bits wide (W for word or w idth)
7
Examples in 4-­‐b it unsigned representation.
15
11 + 2 = ???
14
12
Cannot represent all t he integers
Unsigned values: 0 ... 2W -­‐1
Signed values: -­‐2W-­‐1 ... 2W-­‐1 -­‐1
1101
MSB
3
0011
1000
8
0110010110101001
2
0010
1100
9
“Most-­‐s ignificant” or “high-­‐o rder” bit(s)
0001
11 1011
1010
10
1001
Terminology:
13 + 5 =
1
0000
1110
13
Only 2 W distinct bit patterns...
0
1111
0100
4
0101
5
0110
0111
6
7
x + y in N-­‐bit unsigned arithmetic is (x + y) mod 2 N in math
“Least-­‐s ignificant” or “low-­‐o rder” bit(s)
unsigned overflow = " wrong" answer = wrap-­‐around
LSB
9
= carry 1 out of MSB = math answer too big to f it
11
2
9/8/15
WELLESLEY CS 240
Signed Integers: Sign-­‐Magnitude?
Overflow: Unsigned
Addition overflows
if and only if a carry bit is dropped.
Most-­‐significant bit (MSB) is s ign bit
0 means non-­‐negative
1 means negative
1 1 1
15
+ 2
17
1
1111
+ 0010
10001
15
14
Overflow.
Rest of bits are an unsigned magnitude
1
0000
1110
13
12
0
1111
0001
1101
2
0010
1100
0011
11 1011
1010
10
1001
0100
1000
9
!!!
8-­‐bit s ign-­‐and-­‐magnitude:
3
0x00 = 00000000 represents _____________
0x7F = 01111111 represents _____________
0x85 = 10000101 represents _____________
0x80 = 10000000 represents _____________
4
0101
5
0110
0111
8
6
7
Max and min for N-­‐bit sign-­‐magnitude?
Modular Arithmetic
12
!!!
Sign-­‐Magnitude Negatives
Another problem: cumbersome arithmetic.
Example:
4 -­‐ 3 ! = 4 + (-­‐3)
13
Two’s complement representation
for signed i ntegers
w-­‐bit representation
0100
+1011
–7
–6
–4
1111
0001
1100
0011
1000
–0
Maybe sign-­‐m agnitude is not such a good idea...
+ 2
0010
– 3 1011
1010
–2
1001
-­‐2(w-­‐1)…
w-­‐1 …
+ 1
0000
1101
–1
_ … _… _ _ _ _
+ 0
1110
–5
What about zero?
What is weird about sign-­‐magnitude representation?
22
2
21
1
20
0
weight
position
Positional representation, but
most significant position has negative weight.
0100 + 4
+ 5
0110
23
3
+ 3
0101
0111
2i …
i …
+ 6
+ 7
14
17
3
9/8/15
WELLESLEY CS 240
8-­‐b it representations
0 0 0 0 1 0 0 1
4-­‐b it u nsigned vs. 4-­‐b it two’s complement
1 0 1 1
1 0 0 0 0 0 0 1
1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0
(math) difference = 1 6 = 2 4
11
15
14
1 1 1 1 1 1 1 1
0 0 1 0 0 1 1 1
12
0001
1101
1100
0011
1000
9
Two’s complement: addition Just Works
1
2
0010
+ 3 + 0011
5
0101
Addition overflows
-­‐2
1110
+ -­‐3 + 1101
-­‐5 11011
–1
1110
-­‐2
+ 3 + 0011
1 10001
0
+ 1
1111 0000
1110
0001 + 2
–3
1101
0010
–4
+ 3
1100
0011
0010
2
+ -­‐3 + 1101
1111
-­‐1
0100 + 4
– 5 1011
1010
0101
–6
1001
0110 + 5
1000 0111
–7
+ 6
–8
1100
–7
6
0011
+ 3
0100 + 4
0101
+ 5
0110
1000
0111
–8
7
+ 2
0010
– 5 1011
1010
–6
1001
4
5
0110
0111
0001
+ 6
+ 7
20
Overflow: Two’s Complement
1 1
–2
1 1 1
8
0100
0101
+ 1
0000
1101
–4
3
0
1111
1110
–3
2
0010
11 1011
1010
10
1001
18
–2
1
0000
1110
-­‐5
–1
0
1111
13
1 x -­‐2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0
+ 7
Modular A rithmetic
21
-­‐1
+ 2
1
6
+ 3
9
-­‐7
if and only if the inputs have the same sign but the o utput does n ot.
if and only if the carry in and out of the sign b it differ.
1 1 1
1111
+ 0010
10001
No overflow.
1 1
0110
+ 0011
1001
Overflow.
–1
–2
+ 1
0000
1110
–3
–4
0
1111
0001
1101
1100
0011
– 5 1011
1010
–6
1001
–7
+ 2
0010
1000
–8
+ 3
0100 + 4
0101
+ 5
0110
0111
+ 6
+ 7
Modular Arithmetic
Some CPUs raise exceptions on overflow
C and Java c ruise along silently... Oops?
22
4
9/8/15
WELLESLEY CS 240
A few reasons t wo’s complement is awesome
Another view
Better be true!
x + -x == 0
How s hould w e represent 8 -­‐bit negatives?
• For all positive integers x and w idths n,
Duh!
the n-­‐bit representations of x and –x must sum t o zero.
• Arithmetic should be “the same.”
N-­‐bit negative one is N ones.
Very useful!
Complement rules:
x + ~x == -1
~x + 1 == -x
+
00000001
+
00000000
00000010
00000000
+
00000011
00000000
• Find a rule t o represent –x w here t hat works…
Subtraction is just addition:
4 -­‐ 3 == 4 + (-­‐3)
Great news for hardware.
26
Values To Remember
Conversion Visualized
Two’s Complement → Unsigned
Ordering Inversion
Negative → Big Positive
TMax
Unsigned Values
UMax
UMax – 1
TMax + 1
TMax
UMin
=
000…0
UMax
=
111…1
Unsigned
Range
Two’s Complement Values
0
2w – 1
Values f or W = 32
Decimal
2’s Complement Range
0
–1
–2
0
UMax 4,294,967,296
Hex
FF FF FF FF
11111111 11111111 11111111 11111111
TMax 2,147,483,647
7F FF FF FF
01111111 11111111 11111111 11111111
TMin -­‐2 ,147,483,648
80 00 00 00
10000000 00000000 00000000 00000000
-­‐1
FF FF FF FF
11111111 11111111 11111111 11111111
0
00 00 00 00
00000000 00000000 00000000 00000000
-­‐1
0
TMin
28
TMin
=
–2w–1
100…0
TMax
=
2w–1 – 1
011…1
Negative one
111…1 0xF...F
Binary
29
5
9/8/15
WELLESLEY CS 240
Sign Extension
Sign Extension
Fill new bits with c opies of the sign bit.
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 1 1 1 1 1 0 0
? ? ? ? ? ? ? ? 1 1 1 1 1 1 0 0
0 0 0 0 0 0 1 0
8-­‐b it 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
16-­‐b it 2
1 1 1 1 1 1 0 0
8-­‐b it -­‐4
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
16-­‐b it -­‐4
Try some possibilities…
8-­‐b it 2
16-­‐b it 2
8-­‐b it -­‐4
16-­‐b it -­‐4
Casting from smaller to larger signed type does sign extension.
30
Shift Operations
Left shift: Shift gotchas
x << y
Argument x
01100010
Shift bit vector x left by y positions
Throw away extra bits on left
Fill with 0s on right
x << 3
00010000
Logical: x >> 2
00011000
Right shift: x >> y
Shift bit vector x right by y positions
Throw away extra bits on right
Fill with ??? on left
Logical shift
Fill with 0s on left
Arithmetic shift
Replicate most significant bit on left
31
!!!
For a type represented by n bits, shift by no more than n-­‐1.
C: shift by <0 or >(bits in type) is undefined.
means anything could happen, including computer catching fire
Arithmetic: x >> 2 00011000
Java: shift value is used modulo number of bits in s hifted type
Argument x
10100010
x << 3
00010000
Logical: x >> 2
00101000
given int x: x << 34 == x << 2
Arithmetic: x >> 2 11101000
Why is this useful? Rain check!
!!!
in C: meaning o f >> on s igned types is compiler-­‐d efined! GCC: arithmetic s hift
in Java: >> is arithmetic, >>> is logical
32
6
9/8/15
WELLESLEY CS 240
Using Shifts and Masks
Shifting and Arithmetic: unsigned
Extract 2 nd most s ignificant byte from a 3 2-­‐bit integer:
unsigned
x = 27;
x
x*2n mod 2w
0 0 0 1 1 0 1 1
y = x << 2;
01100001 01100010 01100011 01100100 logical shift left:
0 0 0 1 1 0 1 1 0 0
y == 108
unsigned
1 1 1 0 1 1 0 1
x/2n
Extract the s ign bit of a s igned integer:
shift in zeros from the right
x = 237;
y = x >> 2;
logical shift right:
0 0 1 1 1 0 1 1 0 1
shift in zeros from the left
y == 59
35
34
Shifting and Arithmetic: signed
signed
y = x << 2;
y == 108
x*2n mod 2w
1 0 0 1 1 0 1 1
x = -­‐1 01;
Operands: w bits
*
logical shift left:
1 0 0 1 1 0 1 1 0 0
!!!
Multiplication
True Product: 2*w bits
shift in zeros from the right
u·v
u
•••
v
•••
•••
•••
•••
Discard w bits: w bits
mod 2 w
For x > 0:
x/2n
arithmetic shift right:
shift in copies of MSB from the left
1 1 1 0 1 1 0 1
signed
unsigned: u * v = (u ·∙ v)
overflow iff any b it b y >= w != 0
x = -­‐1 9;
signed: overflow iff any b it b y >= w != b w-­‐1
y = x >> 2;
1 1 1 1 1 0 1 1 0 1
For x < 0 it rounds the opposite direction!
Most programming languages
y == -­‐5
36
More generally true about loss of value when casting to smaller types.
High bits are just discarded.
58
7
9/8/15
WELLESLEY CS 240
Signed vs. Unsigned in C
Power-­‐o f-­‐2 Multiply with Shift
Constants
Operation
u << k = u * 2k
Both signed and unsigned
k
u
Operands: w bits
True Product: w +k bits
*
u·
Discard k bits: w bits
•••
2k
2k
0 ••• 0 1 0 ••• 0 0
•••
UMult w (u , 2k )
TMult w (u , 2k )
Examples
u << 3
(u << 5) - (u << 3)
By default are considered t o be signed integers
Use “U” suffix to force unsigned:
0U, 4294967259U
==
==
•••
0 ••• 0 0
0 ••• 0 0
u * 8
u * 24
59
Signed vs. Unsigned in C
!!!
Casting: bits unchanged, just interpreted differently.
60
!!!
C Casting Surprises
Expression Evaluation
If y ou mix unsigned and signed in a single expression, t hen
signed v alues are implicitly cast to unsigned.
int tx, ty;
unsigned ux, uy;
Explicit casting:
tx = (int) ux;
uy = (unsigned) ty;
Implicit casting via assignments and f unction calls:
tx = ux;
uy = ty;
Including comparison operations <, >, ==, <=, >=
Examples for W = 32: TMIN = -­‐2,147,483,648 TMAX = 2,147,483,647
Constant1
00
-­‐-­‐11
-­‐-­‐11
2147483647
2147483647
2147483647U
2147483647U
-­‐-­‐11
(unsigned)-­‐
(unsigned) 1-­‐1
2147483647 2147483647 2147483647 2147483647 gcc flag -­‐Wsign-­‐conversion warns about implicit casts; -­‐Wall does not!
61
Constant2
Relation
Evaluation
0U
0U
00
0U
0U
-­‐-­‐22147483648 147483648
-­‐-­‐22147483648
147483648
-­‐-­‐22 -­‐-­‐22 2147483648U 2147483648U
(int) (int) 22147483648U 147483648U
==
<
>
>
<
>
>
<
>
unsigned
signed
unsigned
signed
unsigned
signed
unsigned
unsigned
signed
62
8