Representing numbers
This lecture: integers; next time floating point, etc.
Binary representation
Why binary?
Octal and Hex Representations
Mention octal
Conversion of binary to hex
Encourage familiarity with approximate values of
- 0x400 = 2^10 = 1024 = approx. 10^3,
- 0x1000 = 2^12 = 4096,
- 0x8000 = 2^15 = 32768,
- 0x10000 = 2^16 = 65536,
- 0x100000 = 2^20 = approx. 10^6
- 0x40000000 = 2^30 = approx. 10^9
- 0x80000000 = 2^31 = approx. 2 billion.
- 0x100000000 = 2^32 = approx. 4 billion,
Fixed word size:
8, 16 and 32 bits; modular arithmetic.
Aside: n-bit architecture
How many bits (maximum) does the processor access at
one time. Affects: size of registers, size of buses, maximum
addressable memory (often somewhat decoupled using various
architectural "tricks").
Representing negative numbers
- sign and magnitude -- issues
- invert all the bits
- two's complement:
- definition of:
-n is represented by 2^b-n where b is the wordsize.
Note that this is true for both positive and negative n, where |n| < 2^(b-1).
-(2^(b-1)) is representable but 2^(b-1) is not. In total 2^b different
values are representable.
- Simplified computation of two's complement value: subtract from all-1's
value and add 1 == invert each bit and add 1.
- Another simplified computation: subtract 1 from the value and
invert all the bits.
- computing with two's complement numbers: add, subtract, multiply:
treat just like unsigned numbers -- *choose to interpret* as two's complement.
Look at
shifts (explain difference between arithmetic and logical shifts). Sign
extension; note that a negative number always has the high-order bit
set (suggest "prove it").
- ten's complement arithmetic: subtract without borrowing.
- Why do these x-complement number systems work? Proofs:
More about arithmetic
Multiplication: n-bit multiplication can produce a
2n bit result. An n-bit processor may provide the
upper (most-significant) bits in a separate register from the
lower n bits.
Addition or subtraction of two n-bit numbers may produce a
n+1-bit result. How handled? Result is (a+b) mod n. A separate
status register may have a bit set to indicate that a carry
occurred. Typically this bit can be tested with a branch instruction
so software can detected the overflow. Some processors
can associate an interrupt with overflow.
Interpretation of binary data
In past, some computers
tagged memory locations with
the type of the value that they contained.
Modern processors leave interpretation of values to software.
So:
0xFFFFFFFF can be interpreted as -1 (twos complement) or 2^32-1 (unsigned).
Programmers have to be careful when mixing interpretations: the HW
does not help!
Binary data can also be treated as characters:
- 5-bit codes ITA-2 teletype code
- 7-bit codes ASCII (circa-1965)
- 8-bit codes EBCDIC (also circa-1965), various extended ASCII codes including
ISO 8859-1, etc.
- multi/variable byte codes Unicode circa (1990)
Given 8 bits is it a character? a number? part of a number? part of a
character?
You can't tell! It is entirely up to software to determine the
context in which it is used and do the right thing with it.
Exercises
- Convert 783 (base 10) to 16-bit binary and hex and octal
- Convert -783 (base 10) to twos-complement binary and hex and octal (16-bit)
- Add -783 and -75 using 16-bit two's complement arithmetic. Check
your answer using 3-digit 10's complement arithmetic.
- Multiply -783 and -23 using 16-bit two's complement arithmetic. Check
your answer using normal base 10 arithmetic. What happened? Now do the
computation using 32-bit two's complement arithmetic. If you use 10's
complement arithmetic how many digits do you have to use to represent
the multiplicands in order to get a correct answer?