| Arithmetic: addition | |||||||||||
| Unsigned binary addition follows the same rules as ordinary arithmetic | |||||||||||
| Example: 4-bit UB | |||||||||||
| carry | 0 | 1 | 1 | 1 | |||||||
| 0 | 0 | 1 | 1 | 3 | |||||||
| 0 | 1 | 1 | 1 | 7 | |||||||
| result | 1 | 0 | 1 | 0 | 10 | ||||||
| Two's complement addition also follows the same rules as UB | |||||||||||
| Example: 4-bit 2C | |||||||||||
| carry | 1 | 1 | 1 | 1 | |||||||
| 1 | 0 | 1 | 1 | -5 | |||||||
| 0 | 1 | 1 | 1 | 7 | |||||||
| result | 0 | 0 | 1 | 0 | 2 | ||||||
| Note that the carry from the high-order bit may be ignored. | |||||||||||
| How do we subtract? | |||||||||||
| Negate the number to be subtracted and add | |||||||||||
| a - b = a + (-b) | |||||||||||