💡 Data Representation – Computer Arithmetic (Integer Addition and Subtraction)
When we do math in our head, adding or subtracting numbers feels natural.
But inside a computer, things are different.
A computer doesn’t “see” numbers the way we do — it only understands 0s and 1s.
So, how does a machine add or subtract numbers using just these two digits?
Let’s find out.
🧮 1. How Computers Represent Integers
Before a computer can add or subtract, it first needs to represent numbers in binary (base 2).
For example:
- Decimal 5 → Binary 0101
- Decimal 3 → Binary 0011
But what about negative numbers like -5?
That’s where signed number representations come in — such as:
- Sign-Magnitude
- 1’s Complement
- 2’s Complement
Among these, 2’s Complement is the one computers use most often because it makes addition and subtraction super simple.
We’ll mainly use that here.
🔹 2. Binary Addition
Adding binary numbers works almost exactly like decimal addition — just simpler, because you only have 0s and 1s.
Here are the basic rules:
| Binary Addition | Result |
|---|---|
| 0 + 0 | 0 |
| 0 + 1 | 1 |
| 1 + 0 | 1 |
| 1 + 1 | 0 (and carry 1) |
So, if we add 1 + 1, we write 0 and carry 1 to the next column — just like in normal math when 9 + 1 = 10.
🧠 Example 1: Simple Binary Addition
Let’s add 0101 (+5) and 0011 (+3).
0101
+ 0011
--------
1000
Answer = 1000 (which is 8 in decimal)
✅ So, 5 + 3 = 8. Works perfectly!
⚠️ Overflow in Binary Addition
When adding signed numbers, if the result is too big to fit in the number of bits available, we get overflow.
For example (with 4-bit numbers):
0111 (+7)
+ 0001 (+1)
------------
1000 (-8 in 2’s complement)
That looks wrong, right? It’s because the 5th bit (carry) overflowed — our 4-bit system can’t represent +8.
So, overflow means the result is outside the representable range.
🔹 3. Binary Subtraction
Now, subtraction.
You might think computers have a special “subtract” circuit — but actually, they don’t need one!
They perform subtraction using addition.
How?
By adding the 2’s complement of the number you want to subtract.
Let’s see what that means.
✳️ Rule:
A - B = A + (2’s complement of B)
So instead of subtracting directly, the computer flips all the bits of B, adds 1, and then adds that to A.
🧠 Example 2: Subtracting Positive Numbers
Let’s calculate 5 – 3 using 4-bit binary.
- Write 5 →
0101 - Write 3 →
0011 - Find 2’s complement of 3:
- Invert bits:
1100 - Add 1:
1101
- Now add 5 and 2’s complement of 3:
0101
+ 1101
--------
10010
We only keep the last 4 bits → 0010
✅ Result = 2 → correct!
🧠 Example 3: Subtracting a Larger Number (Negative Result)
Let’s try 3 – 5 using 4 bits.
- 3 →
0011 - 5 →
0101 - Find 2’s complement of 5:
- Invert bits:
1010 - Add 1:
1011
- Add 3 and 2’s complement of 5:
0011
+ 1011
--------
1110
1110 in 2’s complement represents -2.
✅ So, 3 – 5 = -2. Perfect again!
🧩 4. Diagram: Integer Addition and Subtraction in Computers
Here’s a simple block diagram showing how the computer performs these operations:
+--------------------------------------+
| Arithmetic Logic Unit |
| (ALU) |
+------------------+-------------------+
|
+-----------------+-----------------+
| |
v v
+---------------+ +-----------------+
| Operand A | | Operand B |
+---------------+ +-----------------+
| |
| v
| +---------------------------+
| | 2’s Complement Circuit |
| | (used when subtracting) |
| +-----------+---------------+
| |
+------------------------------+
|
v
+-----------------+
| Binary Adder |
+-----------------+
|
v
+---------------+
| Result |
+---------------+
⚖️ 5. Summary of Binary Arithmetic Rules
| Operation | Method | Example (4-bit) | Result |
|---|---|---|---|
| Addition | Add normally | 0101 + 0011 | 1000 (+8) |
| Subtraction | Add 2’s complement of subtrahend | 0101 – 0011 | 0010 (+2) |
| Negative result | Comes automatically in 2’s complement | 0011 – 0101 | 1110 (-2) |
| Overflow | Occurs when sign bit changes unexpectedly | 0111 + 0001 | Overflow! |