askvity

How do you add complement numbers?

Published in Binary Arithmetic 3 mins read

Adding numbers represented in complement form, specifically two's complement, involves a straightforward process that leverages binary addition principles. The core idea is to perform standard binary addition and discard any carry-out from the most significant bit (MSB).

Steps for Adding Two's Complement Numbers:

  1. Convert to Binary: Ensure both numbers are represented in binary form using the same number of bits. This is crucial for accurate representation and consistent results. If one number has fewer bits than the other, pad the left side (most significant bits) with zeros. For example, if you're using 8-bit representation, the number 5 (101 in binary) would be represented as 00000101.

  2. Determine Two's Complement (if needed): If either number is negative, convert it to its two's complement representation. To find the two's complement of a number:

    • Invert all the bits (change 0s to 1s and 1s to 0s). This is also known as the one's complement.
    • Add 1 to the result.

    For example, to find the two's complement of -5 in 8 bits:

    • Binary representation of 5: 00000101
    • One's complement: 11111010
    • Add 1: 11111011 (This is the two's complement representation of -5).
  3. Perform Binary Addition: Add the two binary numbers (which may now be in two's complement form) using standard binary addition rules:

    • 0 + 0 = 0
    • 0 + 1 = 1
    • 1 + 0 = 1
    • 1 + 1 = 0, carry-over 1
  4. Discard Carry-Out (if any): If there's a carry-out from the most significant bit (the leftmost bit), simply discard it. This is a key step in two's complement arithmetic.

  5. Interpret the Result: The resulting binary number is the sum in two's complement form. If the MSB of the result is 0, the number is positive. If the MSB is 1, the number is negative and in two's complement form. To find the magnitude of a negative result, take its two's complement again.

Example:

Let's add 5 + (-3) using 8-bit two's complement:

  1. Binary Representation:

    • 5: 00000101
    • 3: 00000011
  2. Two's Complement of -3:

    • Binary of 3: 00000011
    • One's complement: 11111100
    • Add 1: 11111101 (Two's complement of -3)
  3. Binary Addition:

      00000101  (5)
    + 11111101  (-3)
    ----------
     1 00000010
  4. Discard Carry-Out: Discard the leading 1.

  5. Result: 00000010, which is 2 in decimal.

Table Summary:

Step Description
1. Binary Conversion Convert decimal numbers to binary representation.
2. Two's Complement (if negative) Invert bits and add 1 to negative numbers.
3. Binary Addition Add the binary numbers using binary addition rules.
4. Discard Carry-Out Discard the carry-out from the MSB (if any).
5. Interpret Result Convert the resulting binary number to decimal (if needed).

Two's complement arithmetic provides a convenient way to represent and perform addition and subtraction using binary numbers. The discarding of the carry-out bit is a crucial aspect of this system and ensures correct results.

Related Articles