2's Complement Samples

Embed Size (px)

Citation preview

  • 7/29/2019 2's Complement Samples

    1/5

    Computer Organization I Complements

    CMSC 2833 Lecture 5

    1

    Purpose: simplifies binary subtraction

    Binary subtraction requires:

    1. complement2. fixed field widths for binary numbers

    Ones complement:invert all bits

    Example: consider

    10011110, the ones complement is

    01100001

    Think of the ones complement as the difference between the initial operand and a number of equal

    length having a one in every position.

    Example:

    11111111

    - 10011110

    01100001

    Twos complement is one more than the ones complement

    Example: find the twos complement of 10011110

    1. Find the ones complement 01100001

    2. Add one to find the twos complement 01100001

    + 1

    01100010

  • 7/29/2019 2's Complement Samples

    2/5

    Computer Organization I Complements

    CMSC 2833 Lecture 5

    2

    Twos Complement Representation

    1. Choose a field width. Common field widths are 4, 8, 16, 32, and 64 bits.2. A binary number is positive if the most significant digit is zero (0), otherwise it is negative.

    Example: Find the decimal equivalent of the following 16-bit twos complement number

    1001 1100 0000 0101

    1. Make the twos complement number positive. Find the magnitude of the twos complement.1.1. First, find the ones complement by inverting all the bits.

    1001 1100 0000 0101

    0110 0011 1111 1010

    1.2. Next, add one (1) to find the twos complement.0110 0011 1111 1010

    +1

    0110 0011 1111 1011

    2. Convert to decimal.2.1. First convert to hexadecimal.

    0110 0011 1111 1011

    6 3 F B

    2.2. Next, convert to decimal.Hex Dec.

    6 6 163

    24576

    3 3 162

    768

    F 15 161 240

    B 11 160

    11

    25595

  • 7/29/2019 2's Complement Samples

    3/5

    Computer Organization I Complements

    CMSC 2833 Lecture 5

    3

    Complements of Numbers

    rs complement

    NrNn

    cr,)( if 0,0N if 0=N where

    =nnumber of digits in the integer portion of

    N

    =r radix

    Example: Find the tens complement of 147

    c,10)147( 85314710)147(,10,33

    ,10 ==== crn

    Example: Find the tens complement of 0.53

    47.053.010)53.0(,10,0)53.0( 0,10,10 ==== cc rn

    Example: Find the twos complement of 1010

    011010102)1010(,2,4)1010( 24

    ,2,2 ==== cc rn

    =42 10000

    - 1010

    0110

    Example: Find the twos complement of 1010.101

    2,4,)101.1010( ,2 == rnc

    =42 10000. 000

    - 1010. 101

    0101. 011

  • 7/29/2019 2's Complement Samples

    4/5

    Computer Organization I Complements

    CMSC 2833 Lecture 5

    4

    Subtraction with rs complement

    M and Sare two positive base rnumbers were r must be evenly divisible by 2. Find the difference

    SMD =

    1. Define a field width and add M to the sr' complement of .S Discard any digits that carry intopositions more significant than those defined by the field width. Discard any carry out digits.

    Example Find the difference )01111010(

    Find the 2s complement of 0111=S 0110

    1s complement of S 1000

    Add 1 to find 2s complement of S + 1

    2s complement of S 1001

    Add M to the sr' complement of .S M 1010 10

    2s complement of S +1001 -7

    SMD = 10011 3

    Final result 0011 3

    2. Determining the sign.2.1. If the most significant digit,

    2

    rd , then the number is negative, otherwise it is positive.

    2.2. To find the magnitude of a negative number represented in sr' complement form,complement the number.

    Example Find the difference )10100111(

    Find the 2s complement of 0111=S 1010

    1s complement of S 0101

    Add 1 to find 2s complement of S + 1

    2s complement of S 0110

    Add M to the sr' complement of .S M 0111 7

    2s complement of S +0110 -10

    SMD =

    1101 -3

    1s complement 0010

    2s complement 0011 -3

    3. Determining if overflow occurred.3.1. Overflow occurs when adding two positive numbers or when adding two negative numbers.

    When the magnitude of the sum exceeds the range of values that can be represented in the

    field an overflow has occurred.

  • 7/29/2019 2's Complement Samples

    5/5

    Computer Organization I Complements

    CMSC 2833 Lecture 5

    5

    Example: Consider 10s complement representation and a 2-digit field. A number, ,n

    ranges over the interval, .4950 n When the sum is greater than 49 or less than -50an overflow has occurred.

    3.2. Special case: Overflow in 2s complement numbers. Overflow occurs when the carry intothe sign bit is unequal to the carry out.

    Example: consider a 4-bit 2s complement number. A number, ,n ranges over the interval,

    .78 n We consider two possibilities.

    Decimal Binary 2s

    Complement

    10

    -5 0101 1011

    -4 0100 1100

    -9 10111

    Decimal Binary 2s

    Complement

    01

    5 0101 0101

    +4 0100 0100

    9 01001

    Practice Problems.

    1. Convert the following decimal numbers to binary and find the 1s and 2s complements.a.

    15

    b. 123c. 846d. 115.74e. 349.786

    2. Perform the subtraction of the following decimal numbers by using 9s and 10s complements.a. 6251 433b. 834 562c. 3674 598d. 347 491

    3. Using 1s and 2s complements perform the following subtractionsa. 100110 11011b. 1101010 110100c. 10011.1101 101.11d. 1010 - 11011

    4. Find the following differences using 2s complement arithmetic. First convert decimal values tocorresponding binary values. Next, find the twos complement representation of the subtrahend.

    Add the minuend and 2s complemented subtrahend. Check your answers.

    a. 12 6b. 4 6c. 3.125 6.5d. 67.25 83.125