🔢 Binary Arithmetic Tutorial

Master Addition & Subtraction with Signed Numbers

📚 Introduction

Learn how computers perform arithmetic operations on signed binary numbers using efficient 2's complement representation. This system simplifies both addition and subtraction using the same hardware!

🔤 Signed Number Representations

Signed-Magnitude Representation

The leftmost bit represents the sign (0 = positive, 1 = negative), and the remaining bits represent the magnitude.

8-bit Examples:
+14 = 0 0001110 -14 = 1 0001110

Sign bit + Magnitude bits

Signed-2's Complement Representation

Negative numbers are obtained by taking the 2's complement of the positive number. This system is preferred because it simplifies arithmetic operations.

How to find 2's complement:
  1. Invert all bits (1's complement)
  2. Add 1 to the result
+14 = 0 0001110 Invert: 1 1110001 Add 1: 1 1110010 -14 = 1 1110010

Why 2's Complement?

✨ Advantages of 2's Complement:
  • Addition and subtraction use the same hardware
  • No separate positive and negative zero
  • Simpler overflow detection
  • Natural extension of unsigned arithmetic

➕ Binary Addition (2's Complement)

Simple Procedure:

  1. Add the two numbers, including their sign bits
  2. Discard any carry out of the sign bit position
  3. The result is automatically in 2's complement form

Example 1: (+6) + (+13) = +19

Carries: 0 0001110 +6: 0 0000110 +13: +0 0001101 ─────────────── Sum: 0 0010011 (+19)

Example 2: (-6) + (+13) = +7

Carries: 1 1111010 -6: 1 1111010 +13: +0 0001101 ─────────────── Sum: 1 0 0000111 → 0 0000111 (+7) ↑ Carry discarded

➖ Binary Subtraction (2's Complement)

Subtraction is performed by converting it to addition:

Key Concept: (±A) - (±B) = (±A) + (∓B)

Change the sign of the subtrahend and add!

Procedure for A - B:

  1. Take the 2's complement of B (subtrahend)
  2. Add it to A (minuend)
  3. Discard the carry out of the sign bit position

Example: (+7) - (+13) = -6

Step 1: Convert to (+7) + (-13) +7 = 0 0000111 2's complement of +13: +13 = 0 0001101 Invert: 1 1110010 Add 1: 1 1110011 = -13 Step 2: Add them +7: 0 0000111 -13: +1 1110011 ─────────────── Sum: 1 1111010 (-6)

⚠️ Overflow Detection

When two signed n-bit numbers produce a sum requiring n+1 bits, overflow occurs.

Overflow occurs when:
  • Two positive numbers sum to a negative result
  • Two negative numbers sum to a positive result
Detection Method:

If carry into sign bit ≠ carry out of sign bit → Overflow!

This can be implemented with an XOR gate for the two carry signals.

💡 Important Note: Overflow can NEVER occur when adding numbers with unlike signs!

🧮 Interactive Calculator

Enter numbers and click Calculate to see the step-by-step solution!

Explore Signed Magnitude Arithmetic