CMPE1550: Binary Arithmetic with 2’s Complement

πŸ“š Lesson: Binary Arithmetic with 2’s Complement

🎯 Learning Objectives

By the end of this lesson, students will be able to:

  • Explain how 2’s complement represents signed binary numbers.
  • Convert between binary and decimal using 2’s complement.
  • Perform binary addition and subtraction using 2’s complement.
  • Detect and interpret overflow and carry conditions

1️⃣ Review: What Is 2’s Complement

Key Concepts

  • 1’s Complement: Flip all bits (0 β†’ 1, 1 β†’ 0).
  • 2’s Complement: Take the 1’s complement and add 1.
  • Used to represent negative numbers in binary systems.

Range in 8-bit:

Range in 8-bit

Type Binary Range Decimal Range
Positive 00000001 to 01111111 +1 to +127
Zero 00000000 0
Negative 11111111 to 10000000 -1 to -128

2️⃣ Binary Addition Using 2’s Complement

Method

  • Add the binary numbers directly.
  • Ignore the carry out of the MSB.
  • Check for overflow: occurs when the result sign is incorrect.

Example:

  • Add +25 and -12

    +25 β†’ 00011001

    -12 β†’ 11110100 (2’s complement of 00001100)

    Sum β†’ 00001101 β†’ +13 βœ…

Overflow Example:

  • Add +100 and +50

    +100 β†’ 01100100

    +50 β†’ 00110010

    Sum β†’ 10010110 β†’ -106 ❌ Overflow!

3️⃣ Binary Subtraction Using 2’s Complement

Method

  • Convert the subtrahend to its 2’s complement.
  • Add it to the minuend.

Example:

  • Subtract 25 - 12

    25 β†’ 00011001

    12 β†’ 00001100 β†’ 2’s complement β†’ 11110100

    Sum β†’ 00001101 β†’ +13 βœ…

  • Subtract 12 - 25

    12 β†’ 00001100

    25 β†’ 00011001 β†’ 2’s complement β†’ 11100111

    Sum β†’ 11110011 β†’ -13 βœ…

4️⃣ Overflow vs Carry

Condition Meaning in 2’s Complement
Carry Can be ignored in most cases
Overflow Indicates result is out of range

Overflow Detection

  • Happens when adding two numbers with the same sign yields a result with a different sign.

5️⃣ Practice Exercises

  • Addition

    01101001 + 11010010

    11111110 + 01010101

    00110101 + 01110010

  • Subtraction (using 2’s complement)

    01101001 - 11010010

    11111110 - 01010101

    00000010 - 10101010

Make sure you:

  • Show all steps.
  • Indicate if there’s a carry or overflow.
  • Convert results to decimal for verification.

6️⃣ Shortcut for 2’s Complement Conversion

Quick Method:

  • Start from the LSB.

  • Copy bits up to the first 1.

  • Invert all remaining bits.

Example:

Original: 00001100

2’s Complement: 11110100

7️⃣ Wrap-Up Discussion

  • Why is 2’s complement preferred in digital systems?
  • How does overflow differ from carry?
  • What happens when subtraction results in a number outside the representable range?