Binary arithmetic
Addition
Binary addition is calculated by working from right to left and adding columns as you go. If the column adds up to more than 1, then the excess (in binary format) should be carried to the left:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
Number 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 1 |
Number 2 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
Carry | 1 | 1 | ||||||
Answer | 1 | 1** | 0* | 1 | 0 | 1 | 1 | 1 |
* 1 plus 1 is two. In binary, this is ’10’. So write a zero in the column, and carry ‘1’ to the next column.
** 1 plus 2 is three, which in binary is ’11’. So write a ‘1’ in the current column, and carry ‘1’ to the next column.
Subtraction
Binary subtraction is performed by first converting the number to be subtracted into a negative value, and then adding this to the initial value.
For example, to calculate 106 – 24, you would actually perform 106 + (-24).
Subtraction implies that the binary values are signed, meaning the column values look like this:
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
In signed binary, the most significant bit (MSB) is negative. This means that using 8 bits we can represent values from -128 to +127. See here for how to convert a positive number into a negative value.
The example given above would work out as:
-128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | |
Number 1 (106) | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 |
Number 2 (-24) | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 |
Carry | 1 | 1 | 1 | |||||
Result | 0 * | 1 | 0 | 1 | 0 | 0 | 1 | 0 |
*NB in this example, the overflow doesn’t matter
Answer = 64 + 16 + 2 = 82
Multiplication
Multiplication is performed in a similar manner to in standard long multiplication – see the annotated example below. Each row is essentially zero or 1 times the original number, although successively shifted to the left. (The green row in the example).