Integer Addition and Subtraction– Computer Arithmetic

💡 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 AdditionResult
0 + 00
0 + 11
1 + 01
1 + 10 (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.

  1. Write 5 → 0101
  2. Write 3 → 0011
  3. Find 2’s complement of 3:
  • Invert bits: 1100
  • Add 1: 1101
  1. 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.

  1. 3 → 0011
  2. 5 → 0101
  3. Find 2’s complement of 5:
  • Invert bits: 1010
  • Add 1: 1011
  1. 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

OperationMethodExample (4-bit)Result
AdditionAdd normally0101 + 00111000 (+8)
SubtractionAdd 2’s complement of subtrahend0101 – 00110010 (+2)
Negative resultComes automatically in 2’s complement0011 – 01011110 (-2)
OverflowOccurs when sign bit changes unexpectedly0111 + 0001Overflow!