Binary addition and binary shift
When two numbers are added together in denaryThe number system most commonly used by people. It contains 10 unique digits 0 to 9. Also known as decimal or base 10., we take the first number, add the second number to it, and get an answer. For example, 1 + 2 = 3.
When we add two binaryA number system that contains two symbols, 0 and 1. Also known as base 2. numbers together the process is different.
There are four rules that need to be followed when adding two binary numbers. These are:
- 0 + 0 = 0
- 1 + 0 = 1
- 1 + 1 = 10 (binary for decimal 2)
- 1 + 1 + 1 = 11 (binary for decimal 3)
Example - adding 01 + 10
1 + 0 = 1
0 + 1 = 1
Result in binary - 11 (which is decimal 3)
Example - adding 01 + 101
1 + 1 = 0, carry 1
1 + 0 + 0 = 1
0 + 1 = 1
Result in binary - 110 (which is decimal 6)
Example - adding 01010011 + 01110110
1 + 0 = 1
1 + 1 = 0, carry 1
1 + 0 + 1 = 0, carry 1
1 + 0 + 0 = 1
1 + 1 = 0, carry 1
1 + 0 + 1 = 0, carry 1
1 + 1 + 1 = 1, carry 1
1 + 0 + 0 = 1
Result in binary - 11001001 (which is decimal 201). You can check your answers by converting each binary number into decimal and checking your addition. In this example, 01010011 is 83 in decimal and 01110110 is 118 in decimal. So, 83 + 118 is 201.
Adding 3 binary numbers
If we need to add 3 binary numbers together, the same addition rules apply.
Example – adding 00010001 + 01010101 + 00000001
1 + 1 + 1 = 1, carry 1
0 + 0 + 0 + 1 = 1
0 + 1 + 0 = 1
0 + 0 + 0 = 0
1 + 1 + 0 = 0, carry 1
0 + 0 + 0 = 0
0 + 1 + 0 = 1
0 + 0 + 0 = 0
Overflow
Overflow occurs when the result of a calculation requires more bits - place values - than are in the available range.
For example, when using eight bits, the largest number that can be recorded is 11111111 (decimal 255). When adding together two eight-bit numbers, a situation may occur when the result requires more than eight bits to hold it. For example, adding the binary numbers 11111110 (decimal 254) and 00000010 (decimal 2) would give:
The result is actually 10000000 (decimal 256), which requires nine bits. However, as only eight bits are available to hold the number, the result would be 00000000 (decimal 0).
As you can see, overflow can have serious consequences for the validity of calculations.
Binary shifts
Binary numbers are multiplied and divided through a process called shifting.
Multiplication
To multiply a number, a binary shift moves all the digitA single whole number value from 0 to 9, especially when used in a larger number. For example, the number 752 has 3 digits. in the binary number along to the left and fills the gaps after the shift with 0:
- to multiply by two, all digits shift one place to the left
- to multiply by four, all digits shift two places to the left
- to multiply by eight, all digits shift three places to the left
- and so on
Example - 1100 (decimal 12) × 2
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 1 | 0 | 0 |
| 128 | |
|---|---|
| 64 | |
| 32 | |
| 16 | |
| 8 | 1 |
| 4 | 1 |
| 2 | 0 |
| 1 | 0 |
Result - shifting one place to the left gives 11000 (decimal 24)
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 1 | 0 | 0 | 0 |
| 128 | |
|---|---|
| 64 | |
| 32 | |
| 16 | 1 |
| 8 | 1 |
| 4 | 0 |
| 2 | 0 |
| 1 | 0 |
Example - 10110 (decimal 22) × 4
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 0 | 1 | 1 | 0 |
| 128 | |
|---|---|
| 64 | |
| 32 | |
| 16 | 1 |
| 8 | 0 |
| 4 | 1 |
| 2 | 1 |
| 1 | 0 |
Result - shifting two places to the left gives 1011000 (decimal 88)
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 | 0 |
| 128 | |
|---|---|
| 64 | 1 |
| 32 | 0 |
| 16 | 1 |
| 8 | 1 |
| 4 | 0 |
| 2 | 0 |
| 1 | 0 |
Multiplying and dividing binary numbers using binary shifts