3 Data Integer Representation V1
3 Data Integer Representation V1
6
Representing unsigned integers
• Given n bits to store an integer, we can represent 2n different values
• If we just care about non-negative (aka unsigned) integers, we can easily
store the values
0, 1, 2, …, 2n-1
• E.g., for 4 bits
• 0x2 = 2
• 0xB = 11
• 0xF = 15 = 24-1
• Binary number
7
Representing negative integers
• We have seen how to represent unsigned integers (i.e., non-negative
integers) as unsigned binary numbers
• Every number between 0 and 2w-1 has a unique encoding as a w-bit value
• Addition works as we expect it to
• How do we represent negative integers?
• Three common encodings:
• Sign and magnitude
• Ones’ complement
• Two’s complement
8
Sign and magnitude
• Use one bit to represent sign, remaining bits represent magnitude
• With n bits, have n-1 bits for magnitude
• E.g., with 4 bits, can represent integers
-7, -6, …, -1, 0, 1, …, 6, 7
• MSB (most significant bit) represents the sign
• 0 is positive
• 1 is negative
1011 represents -3
sign: - magnitude: 3
9
Properties of sign and magnitude
• Advantages:
• Straight-forward and intuitive
• Issues:
• Arithmetic operations need different implementations for signed and unsigned
numbers
• E.g., addition, using 4 bits
• unsigned: 0001 + 1001 = 1 + 9 = 10 = 1010
• sign and magnitude: 0001 + 1001 = 1 + -1 = 0, which is not 1010
• Two different representations of zero!
• E.g., using 4 bits, 1000 and 0000 both represent zero!
10
Binary Ones’
0
0001 1
• Negation is performed by performing a bitwise not (~) on
the number 0010 2
• In other words, flip all the bits in the number 0011 3
• For example, using 4 bit numbers: 0100 4
• 6= 0110
• -6 = 1001 0101 5
• Like sign and magnitude, first bit indicates whether number 0110 6
is negative 0111 7
• If the msb (most significant bit) is 0, treat it like an unsigned binary 1000 -7
number
1001 -6
• If the msb is 1, the number is negative, flip all the bits to see its
magnitude 1010 -5
• Using n bits, can represent numbers 2n-1 values 1011 -4
• E.g., using 4 bits, can represent integers 1100 -3
-7, -6, …, -1, 0, 1, …, 6, 7 1101 -2
1110 -1
1111 -0
11
Properties of ones’ complement
• We still have two different representations of zero
• E.g., using 4 bits, 1111 and 0000 both represent zero
• Same implementation of arithmetic operations for signed and unsigned!
• E.g., addition using 4 bits
• unsigned: 0001 + 1001 = 1 + 9 = 10 = 1010
• ones’ complement: 0001 + 1001 = 1 + -6 = -5 = 1010
0000 0 0
• Take the ones’ complement of the number and 0001 1 1
add one 0010 2 2
• Flip all the bits and add one to the number 0011 3 3
15
Two’s Complement Interpretation
• You can interpret a two’s complement number as having a “negative
weighting” in the MSB
-23 = -8 22 = 4 21 = 2 20 = 1
-8 +4 +2 +1
16
Quick two’s complement conversion
• One trick for taking the to two’s complement of a number quickly
• Flip all the bits, then starting from the lsb, flip all the 1s you see to 0s until you get
to a 0, flip that 0 to a 1 and stop
• Example: using 6-bit two’s complement numbers, convert 4 to -4
• Write out 4 in binary using 6 bits
• Flip all the bits
• Flip the rightmost 1’s and the first 0
• For negative two’s complement numbers, leading 1s are similar to leading
0s for positive numbers
17
Signed vs. unsigned in C
• int x; // declares x as a signed integer
• unsigned int x; // declares x as an unsigned integer
• Constants are signed by default
• 42 // would be stored as a two’s complement number
• 42u // would be stored as an unsigned binary number
• C allows conversion between unsigned and signed
• Most systems follow the rule that the underlying bit representation does not
change
int main(void) {
unsigned char ux = 0xff;
char x = ux;
printf("%d %d\n", ux, x);
}
18
Mapping Signed Unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4
= 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 +/- 16 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
15
Relation between Signed & Unsigned
w–1 0
ux + + + ••• + + +
x - + + ••• + + +
16
Expanding the bit representation of a number
• How do you convert from a smaller integer data type to a
larger integer data type (e.g., a short to an int) while
retaining the same value?
• Should always be possible because the larger type will have a
wider range of numbers than the smaller type
• To convert an unsigned number to a larger data type:
• Add leading zeros to the representation
• Known as zero extension
• To convert a two’s complement number to a larger data
type:
• Perform sign extension
• Add copies of the msb (the sign bit) of the smaller
representation to the extra bits of the larger representation
19
Truncating Numbers
• When reducing the number of bits in a number, the smaller number may
not be able to correctly represent the larger number
• We reduce the number of bits representing a number by truncating the
uppermost (high-order) bits
• Reinterpret this truncated number
• For unsigned numbers, it is equivalent to performing a mod 2k on the
original value where k is the smaller bit width
• A similar property holds for signed numbers except that it then converts
the msb into a sign bit
• Which means it is possible that truncating a signed number changes its sign
• Rule of thumb: if the larger number is within the expressible range of the
smaller number, the conversion will yield a correct result
20
Expanding and Truncating Rules
• Expanding (e.g., short to int)
• Unsigned: zeros added
• Signed: sign extension
• Both yield expected result
19
Values for Different Word Sizes
W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808
• Observations
• |TMin | = TMax + 1
• Asymmetric range
• Umax = 2 * TMax + 1
20
What is going on with this cartoon?
21