How to Add and Subtract Binary Numbers With Two's Complement for Beginners
Concept
Digital Logic
Binary
Math
Understanding binary math is a key concept in computer science, digital electronics, and programming. Unlike the base-10 system that you use every day, binary (base-2) arithmetic can feel confusing at first. But with a little practice, performing binary addition and binary subtraction becomes easy to understand. These operations are necessary when trying to learn how computers process data and perform calculations at the most basic level.
The Basics of Math With Binary
How to Count in Binary
Binary counting is similar to decimal counting, but in a base-2 number system. There are only two digits in binary: 1 and 0. When counting in binary, we start at the lowest digit (0) and go to the highest digit (1). When we reach 1, we carry a 1 to the next column on the left. To demonstrate, let's count from 0 to 4 in binary:
000 (0) 001 (1) 010 (2) carry over to the next column 011 (3) 100 (4) carry over to the next column
In binary, carries happen every two numbers, as there are 2 possible digits. Like the decimal system, the carry happens one column to the left.
How to Add in Binary
Binary math is very similar to decimal math. Noting this, let's do a quick review of decimal addition. Our example will be:
53 + 29
To start, let's take a look at the ones column. 3 + 9 equals 12. Because 12 is bigger than 9 (base-10), we are going to have to carry the 1 over to the tens column. To get the final result, we add 5 + 2 + 1 and get 8. Our final answer:
53 + 29
For our example in binary, let's use
0101 + 1111
Adding in binary can get to be complex when there's a lot of carrying, so let's take a look at a way to write this out simply.
- Start from the rightmost bit (least significant bit)
- Add the digits together. If the sum is 2 (1+1), carry a one to the next column.
- If the sum with the carry is 3 (1+1+1), write down a 1 and carry a 1.
- Repeat for all digits.
Looking at our example, let's put this into practice:
1 1 + 10 0 1 + 10 1 1 + 11 0 1 + 10100
We get 10100. To check our work, let's convert the binary to decimal:
5 (0101) + 15 (1111) = 20 (10100)
Binary Subtraction Using Two's Complement
Binary subtraction utilizes something called the two's complement. The two's complement is a way of representing a positive or a negative in a binary number based on the leading (leftmost) digit. In two's complement:
- A leading 0 is a positive number.
- A leading 1 is a negative number.
- 0 is still represented as "0000."
A 4-bit number can represent values from -8 to 7. For example:
0110 positive: leading bit is 0 1111 negative: leading bit is 1
Using the two's complement, we can turn addition into subtraction like so:
5 + -3 = 2
To find the two's complement of a number, we need to do the following:
- Invert each bit (perform a NOT Boolean operation)
- Add one to the resulting number
The leading bit is still a 1, but now we ended up with a 6-digit number. If we take away this extra bit, we get two in both operations. For our purposes, we will simply ignore this bit and say we got 00010.
Conclusion
Binary math may seem unfamiliar, but it uses the same concepts as base-10 math does. Once you understand carrying, two's complement, and base conversions, you can add or subtract in any number system. A great next step would be to try adding and subtracting in hexadecimal on your own. If you're unfamiliar with hexadecimal, check out this article that explains binary and hexadecimal for beginners. The best way to get better at these sorts of things is to find what works best for you. Try new ways to add or subtract binary numbers and see what works best for you.