| Integers: 2's complement | ||||||||||||||
| 2's complement (2C) may not be intuitively obvious, but it has nice properties | ||||||||||||||
| Negation | ||||||||||||||
| 1. flip all bits | ||||||||||||||
| 2. add 1 (ignoring any carry out of the msb) | ||||||||||||||
| Example: 11ten is 01011 in 5-bit unsigned binary. Call this number B. | ||||||||||||||
| 1. flip bits: | 10100 | ~B | ||||||||||||
| 2. add 1: | + 1 | |||||||||||||
| 10101 | -B | |||||||||||||
| What do we get if we add this to the original number? | ||||||||||||||
| 01011 | B | |||||||||||||
| + | 10101 | + -B | ||||||||||||
| 00000 | 0 | |||||||||||||
| If we negate B (-B) using 2C and negate again (--B) we get the original value B back. | ||||||||||||||
| 1. flip bits: | 01010 | ~(-B) | ||||||||||||
| 2. add 1: | + 1 | |||||||||||||
| 01011 | B | |||||||||||||
| Other ways to do N-bit 2C negation: | ||||||||||||||
| 1. Find the rightmost 1 bit, then flip all the higher bits (bits to the left). | ||||||||||||||
| Example: | 01011 | |||||||||||||
| Rightmost 1 bit is b0 | ||||||||||||||
| flip bits to the left: | 10101 | |||||||||||||
| Why does this work? | ||||||||||||||
| Consider N-bit value value: B = bN-1bN-2 . . . 1 0 . . . 0 | ||||||||||||||
| where bk is 1 and all bi are 0 for i < k | ||||||||||||||
| If we create the negation -B by flipping the bits to the left of bk, then | ||||||||||||||
| -B = ~bN-1~bN-2 . . . 1 0 . . . 0 | ||||||||||||||
| What happens when we add these 2 numbers? | ||||||||||||||
| For each bi, i < k, the sum of the bits is 0 + 0 = 0 | ||||||||||||||
| For bit k, the sum of the bits is 1 + 1 = 10, so the result is 0 with carry 1 | ||||||||||||||
| Now, for i > k, each bit is either 0 or 1 in B and the opposite in -B | ||||||||||||||
| So, the result is 1 + 0 + carry bit 1 for a result of 0 and a carry 1 | ||||||||||||||
| For the leftmost bit position N-1, the result is 0, and the final carry is ignored. | ||||||||||||||
| Therefore, the result consists of all 0's, which must be true for B + (-B) | ||||||||||||||
| 2. Subtract B from value: 1 followed by N 0's. | ||||||||||||||
| Example: | 01011 | |||||||||||||
| subtract from 100000: | 100000 | |||||||||||||
| - 01011 | ||||||||||||||
| 10101 | ||||||||||||||
| Odometer (Ferris Bueller) view: | Ferris Bueller's Day Off | |||||||||||||
| Consider a 4-digit decimal odometer. | ||||||||||||||
| What is the largest milage it can show? | ||||||||||||||
| 9999 | ||||||||||||||
| What happens if we go 1 more mile? | ||||||||||||||
| 0000 | ||||||||||||||
| Now, think of starting at 0000 and going backward, like Ferris: | ||||||||||||||
| Go back 1 mile, and the odometer reads 9999, 2 miles 9998, and so forth. | ||||||||||||||
| N-bit 2's complement is really like a binary odometer: | ||||||||||||||
| Go forward from 00 . . . 0 for positive numbers. | ||||||||||||||
| Go backward for negative numbers. | ||||||||||||||
| What's really going on here mathematically? | ||||||||||||||
| What is the value of 1 followed by N 0's? 2N | ||||||||||||||
| So, N-bit 2C is really based on arithmetic modulo 2N | ||||||||||||||
| This is why 2C has nice properties for doing arithmetic. | ||||||||||||||
| Advantages of 2C representation: | ||||||||||||||
| Only 1 zero | ||||||||||||||
| Add using same hardware as unsigned binary | ||||||||||||||
| Subtract by taking complement and adding. | ||||||||||||||
| Why is there only 1 zero? | ||||||||||||||
| Consider the 5-bit positive zero 00000 | ||||||||||||||
| 1. flip bits: | 11111 | |||||||||||||
| 2. add 1: | + 1 | |||||||||||||
| 00000 | ||||||||||||||
| This is negative 0, but it has the same representation :) | ||||||||||||||
| Range of values: | ||||||||||||||
| Maximum positive number: 2N-1 - 1 | ||||||||||||||
| Minimum negative number: - (2N-1) | ||||||||||||||
| Total number of values (including 0): 2N | ||||||||||||||
| Converting from base 10 to N-bit 2C | ||||||||||||||
| 1. Convert to N bits UB | ||||||||||||||
| 2. If number has minus sign, negate: | ||||||||||||||
| flip bits | ||||||||||||||
| add 1 | ||||||||||||||
| Converting from N-bit 2C to base 10 | ||||||||||||||
| 1. If msb is 1 (negative number), negate | ||||||||||||||
| 2. Convert result (or original value) as UB to base 10 | ||||||||||||||
| 3. If number was negative, insert minus sign | ||||||||||||||
| Conclusion: 2C rules! | ||||||||||||||
| Most computer representations of signed integers use 2C. | ||||||||||||||