2.5 - Binary Arithmetic
Adding binary numbers using column addition
Binary addition follows similar rules to decimal addition but uses only the digits 0 and 1. This process involves aligning numbers in columns and adding from right to left, with specific rules for carries due to the base-2 system.
Rules for binary addition
- 0 + 0 = 0 (no carry)
- 0 + 1 = 1 (no carry)
- 1 + 0 = 1 (no carry)
- 1 + 1 = 10 (write 0, carry 1 to the next column)
- 1 + 1 + 1 = 11 (write 1, carry 1 to the next column) – this can occur if there is already a carry from the previous column
These rules ensure that sums exceeding 1 in a column produce a carry to the next higher place value.
Steps for column addition in binary:
- Align the binary numbers vertically, with the least significant bit (rightmost) in the same column.
- Start adding from the rightmost column, moving left.
- For each column, add the bits plus any carry from the previous column.
- Write the result bit in the current column and carry over if necessary.
- Continue until all columns are added, including any final carry.
Worked example - Adding two 8-bit binary numbers
Add the 8-bit binary numbers 1010 0110 and 0011 1001.
Step 1: Align the numbers
Step 2: Add column by column starting from the right
- Column 1 (rightmost): 0 + 1 = 1 (no carry)
- Column 2: 1 + 0 = 1 (no carry)
- Column 3: 1 + 0 = 1 (no carry)
- Column 4: 0 + 1 = 1 (no carry)
- Column 5: 0 + 1 = 1 (no carry)
- Column 6: 1 + 1 = 10 (write 0, carry 1)
- Column 7: 0 + 0 + carry 1 = 1 (no carry)
- Column 8: 1 + 0 = 1 (no carry)
Step 3: Write the result
The sum is 1101 1111.
Understanding overflow errors in binary addition
An overflow error happens when the result of a binary addition requires more bits than the fixed number available, such as in an 8-bit system. This occurs because computers allocate a specific number of bits for numbers, and exceeding this limit can lead to incorrect results.
Causes and effects of overflow errors:
- Cause - The sum produces a carry that creates an extra bit beyond the allocated size (e.g., adding two 8-bit numbers resulting in a 9-bit number).
- Detection - The most significant bit (leftmost) is often the one that overflows. Computers use overflow flags to signal this error.
- Consequences:
- Loss of data, as the extra bit may be discarded, leading to an inaccurate result (e.g.,
1111 1111+0000 0001=1 0000 0000, but an 8-bit system might output0000 0000). - Potential software crashes if the system cannot handle the error.
- Loss of data, as the extra bit may be discarded, leading to an inaccurate result (e.g.,
Computers may store overflow bits elsewhere or use error-handling mechanisms to mitigate these issues.
Worked example - Identifying overflow in binary addition
Add the 8-bit binary numbers 1110 0011 and 1011 0101, giving the result as an 8-bit number and explaining any issues.
Step 1: Align the numbers
Step 2: Add column by column starting from the right
- Column 1: 1 + 1 = 10 (write 0, carry 1)
- Column 2: 1 + 0 + carry 1 = 10 (write 0, carry 1)
- Column 3: 0 + 1 + carry 1 = 10 (write 0, carry 1)
- Column 4: 0 + 0 + carry 1 = 1 (no carry)
- Column 5: 0 + 1 = 1 (no carry)
- Column 6: 1 + 1 = 10 (write 0, carry 1)
- Column 7: 1 + 0 + carry 1 = 10 (write 0, carry 1)
- Column 8: 1 + 1 + carry 1 = 11 (write 1, carry 1 – this creates a 9th bit)
Step 3: Write the full result and handle overflow
The full sum is 1 1001 1000. Ignoring the overflow bit gives the 8-bit result 1001 1000.
Step 4: Interpretation
This is an overflow error, as the result requires 9 bits. Discarding the extra bit leads to data loss and inaccuracy, which could cause software issues if not managed.
Performing binary shifts for multiplication and division
A binary shift, also called a logical shift, moves all bits in a binary number left or right by a specified number of places. Empty positions are filled with 0s. Shifts provide a quick way to multiply or divide by powers of 2, as binary is base-2.
Types of binary shifts:
- Left shift - Moves bits to the left, effectively multiplying the number by 2 for each place shifted (e.g., one left shift doubles the number).
- Right shift - Moves bits to the right, effectively dividing the number by 2 for each place shifted (e.g., one right shift halves the number, using integer division that discards remainders).
Shifting n places left multiplies by , while shifting n places right divides by .
Potential issues with binary shifts:
- Left shift overflows - If the shift pushes bits beyond the allocated size, data is lost, and an overflow flag may be set.
- Right shift bit loss - Bits shifted off the right end are discarded, which can reduce accuracy (similar to integer division ignoring remainders).
- These issues can lead to loss of precision in calculations.
Worked example - Performing a left binary shift
Perform a 2-place left shift on the 8-bit binary number 0001 1100. Explain the effect and any potential problems.
Step 1: Write the original number
0001 1100
Step 2: Shift bits 2 places left and fill with 0s
- Shifted result:
0111 0000(bits move left, rightmost two positions become 0)
Step 3: Calculate the effect
The number has been multiplied by (doubled twice).
Step 4: Interpretation
If limited to 8 bits, no overflow occurs here, but further shifts could cause bits to overflow from the left, leading to data loss and reduced accuracy.
Worked example - Performing a right binary shift
Perform a 3-place right shift on the 8-bit binary number 0110 1000. Explain the effect.
Step 1: Write the original number
0110 1000
Step 2: Shift bits 3 places right and fill with 0s
- Shifted result:
0000 1101(bits move right, leftmost three positions become 0; rightmost three bits are discarded)
Step 3: Calculate the effect
The number has been divided by (halved three times, using integer division).
Step 4: Interpretation
The discarded bits may cause a loss of precision, as any remainder from the division is ignored, similar to the DIV operator in programming.