Method:
Here's how to do binary subtraction:
- Turn the second binary number to its 2's complement, negative form. You can review how to do this here.
- Add the binary numbers.
Turning a binary number to its negative form
- Start from the right-most digit (least significant bit).
- Working your way left write every digit the same untill you get to a 1. Then invert the rest of the digits(0 to 1 or 1 to 0).
Adding 2 binary numbers
Here's how to add two 8-bit binary numbers:
- Line up the numbers: Write the binary numbers vertically, aligning the rightmost digits (least significant bits). Use zeros to pad shorter numbers.
- Add each digit pair: Add the corresponding digits (0 + 0, 0 + 1, 1 + 0, 1 + 1) following these rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0, carry 1
- 1 + 1 + 1(carry) = 1, carry 1
- Check for carry:
- If the sum of two digits is 1, write down 1 as the result digit.
- If the sum is 2 (1 + 1), write down 0 as the result digit and carry 1 to the next column.
- Add the carry: If there's a carry from the previous column, add it to the sum of the next digit pair before checking for carry again.
- Repeat steps 2-4: Continue adding digits and checking for carry until you reach the leftmost digits.
Carry Forward
Carry forward happens when adding two binary digits results in a sum greater than 1. In such cases:
- You write down 0 as the result digit for the current column.
- You carry 1 to the next column on the left.
- This "carried 1" is essentially added to the next digit pair in the next column.