CS 61C Number Representation Spring 2020: 1 Unsigned Integers
CS 61C Number Representation Spring 2020: 1 Unsigned Integers
1 Unsigned Integers
1.1 If we have an n-digit unsigned numeral dn−1 dn−2 . . . d0 in radix (or base) r, then
Pn−1
the value of that numeral is i=0 ri di , which is just fancy notation to say that
instead of a 10’s or 100’s place we have an r’s or r2 ’s place. For the three radices
binary, decimal, and hex, we just let r be 2, 10, and 16, respectively.
We don’t have calculators during exams, so let’s try this by hand. Recall that our
preferred tool for writing large numbers is the IEC prefixing system:
(a) Convert the following numbers from their initial radix into the other two
common radices:
1. 0b10010011
2. 63
3. 0b00100100
4. 0
5. 39
6. 437
7. 0x0123
1. 0xD3AD
2. 0xB33F
3. 0x7EC4
• 2 Ki • 512 Ki • 16 Mi
• 256 Pi • 64 Gi • 128 Ei
2 Number Representation
2 Signed Integers
2.1 Unsigned binary numbers work for natural numbers, but many calculations use
negative numbers as well. To deal with this, a number of different schemes have
been used to represent signed numbers, but we will focus on two’s complement, as it
is the standard solution for representing signed integers.
• Most significant bit has a negative value, all others are positive. So the value of
Pn−2
an n-digit two’s complement number can be written as i=0 2i di − 2n−1 dn−1 .
• A neat trick for flipping the sign of a two’s complement number: flip all the
bits and add 1.
For questions (a) through (c), assume an 8-bit integer and answer each one for the
case of an unsigned number, biased number with a bias of -127, and two’s complement
number. Indicate if it cannot be answered with a specific representation.
(a) What is the largest integer? What is the result of adding one to that number?
1. Unsigned?
2. Biased?
3. Two’s Complement?
1. Unsigned?
2. Biased?
3. Two’s Complement?
1. Unsigned?
2. Biased?
3. Two’s Complement?
(d) What is the largest integer that can be represented by any encoding scheme
that only uses 8 bits?
Number Representation 3
(e) Prove that the two’s complement inversion trick is valid (i.e. that x and x + 1
sum to 0).
(f) Explain where each of the three radices shines and why it is preferred over
other bases in a given context.
(a) Compute the decimal result of the following arithmetic expressions involving
6-bit Two’s Complement numbers as they would be calculated on a computer.
Do any of these result in an overflow? Are all these operations possible?
1. 0b011001 − 0b000111
2. 0b100011 + 0b111010
3. 0x3B + 0x06
4. 0xFF − 0xAA
(b) What is the least number of bits needed to represent the following ranges using
any number representation scheme.
1. 0 to 256
2. -7 to 56