Data Representation

Computer memory stores only sequences of 0's and 1's. In order for data to be meaningful we must agree how to encode that data with 0's and 1's. Unfortunately, there are several systems that are used.

Unsigned Integers
- Simply use binary
- Using N bits, the integers 0..2^(N)-1 can be represented.
Examples-17
Signed Magnitude
- Use the first bit for a sign (0 for +, 1 for -)
- Use the remaining N-1 bits for the magnitude (absolute value), in binary
- For N bits, -(2^(N-1)-1) to +(2^(N-1)-1) can be represented.
- Simple, but arithmetic circuits become awkward
- Two different zeroes are created (+0 is 000..00 and -0 is 100..00)
- Arithmetic is awkward eg. add 0101 with 1110. Since signs are different, determine the larger whose sign will be kept for answer, subtract larger from smaller. Not pretty
-Examples: +15, -6
Excess-N
- To represent X, use the binary code for X+M
- Excess-2^(N-1) is normal, but any value of M can be used
- The code for zero is 100..00 in Excess-2^(N-1)
- First bit is still a sign bit in Excess-2^(N-1), but 1 is + and 0 is -
- Excess-2^(N-1) is almost the same as Twos Complement (differ in sign only)
- Excess-2^(N-1) with N bits can represent -2^(N-1) to +2^(N-1)-1
Example: For 4 bits use excess-8 notation -8,-7,0,7
Ones Complement
- Positive numbers begin with a 0, otherwise use normal binary codes
- Negative numbers use the complement (toggle all bits) of the code for the corresponding positive number
- For N bits, -(2^(N-1)-1) to +(2^(N-1)-1)
- Two different zeroes are created (+0 is 000..00 and -0 is 111..11?)
Example: 5, -5
Twos Complement
- Positive numbers begin with a 0, otherwise use normal binary codes
- Negative numbers use the "twos complement" of the code for the corresponding positive number
- To "twos complement" something:
1. Toggle every bit and then add 1 to the result (in binary)
OR
2A. Leave the lowest 1 and trailing zeroes alone. Toggle the other bits.
OR
2B. Toggle any bit which has a 1 bit to its right.
- Zero is represented ONLY by 00...00 (the only code which does this).
- The "twos complement" operation is self-inverting. Applied to any number (positive or negative) it will negate it (exception: -2^(N-1) cannot be negated).
- The first bit is a sign bit (0 for +, 1 for -)
- Using N bits -(2^(N-1)) to +(2^(N-1)-1) can be represented
Example: 5,-5,4,-4 in all styles

The 4-bit codes for signed integers in these formats:

 Value Signed Magnitude Excess-7 Excess-8 Excess-9 Ones Complement Twos Complement
-9 ---- ---- ---- 0000 ---- ----
-8 ---- ---- 0000 0001 ---- 1000
-7 1111 0000 0001 0010 1000 1001
-6 1110 0001 0010 0011 1001 1010
-5 1101 0010 0011 0100 1010 1011
-4 1100 0011 0100 0101 1011 1100
-3 1011 0100 0101 0110 1100 1101
-2 1010 0101 0110 0111 1101 1110
-1 1001 0110 0111 1000 1110 1111
 0 1000 or 0000 0111 1000 1001 1111 or 0000 0000
+1 0001 1000 1001 1010 0001 0001
+2 0010 1001 1010 1011 0010 0010
+3 0011 1010 1011 1100 0011 0011
+4 0100 1011 1100 1101 0100 0100
+5 0101 1100 1101 1110 0101 0101
+6 0110 1101 1110 1111 0110 0110
+7 0111 1110 1111 ---- 0111 0111
+8 ---- 1111 ---- ---- ---- ----