Twos Complement - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Twos Complement

Description:

Two's Complement Tutorial S.Preuitt CS160. 2. Example done in class ... Note that the resulting bit-pattern should be the same length as the problem, so, ... – PowerPoint PPT presentation

Number of Views:283
Avg rating:3.0/5.0
Slides: 18
Provided by: Preu
Category:

less

Transcript and Presenter's Notes

Title: Twos Complement


1
Twos Complement
  • "Counting in binary is just like counting in
    decimal if you are all thumbs."    
  •  Glaser and Way.

2
Example done in class
  • Convert -1310 to 2s complement
  • First thing to notice
  • it is a negative number, so, left-most bit has
    to be a 1 in the answer
  • Second thing to notice
  • it cannot be represented with just 4 bits as
    this will lead to an overflow

3
-1310 to 2s complement
  • Convert 1310 to binary using division algorithm
  • 11012 in 4-bit notation
  • Before doing anything, notice that 4-bit causes
    overflow in 2s complement notation for negative
    numbers smaller than -8 and positive numbers
    greater than 7. So, represent 1310 in 8-bit
    notation.
  • 0000 11012
  • 3. Now, convert this 8-bit binary (00001101) to
    2s complement

4
-1310 to 2s complement continued
  • Convert 0000 11012 to 1s complement
  • 1111 00102
  • Add 1 to 1s complement to get 2s complement
  • 1111 00102
  • 0000 00012
  • 1111 00112
  • 2s complement of -1310 is therefore

1111 00112
5
Another example convert -1710 to 2s complement
  • First notice that it is a negative number, so,
    final answer should have the left-most bit as 1
  • Convert 1710 to binary using division algorithm
  • 0001 00012
  • 1s complement the above binary string
  • 1110 1110
  • Add 1 to the above 1s complement
  • 1110 1111
  • Therefore the 2s complement of -1710 is

1110 1111
6
Summary Decimal to 2s Complement
  • Choose the appropriate number of bits for the
    bit-representation in binary to avoid overflow
  • For a positive decimal number, to convert to 2s
    complement, express it in appropriate binary bits
    and stop.
  • For a negative decimal number, to convert to
    2complement,
  • First convert the binary to 1s complement
  • Then add 1 to this 1s complement
  • The result is the 2s complement of the given
    negative number

7
Another Example
  • Convert 1310 to 2s complement
  • First thing to notice
  • it is a positive number, so, left-most bit has
    to be a 0 in the answer
  • Second thing to notice
  • it cannot be represented with just 4 bits as
    this will lead to an overflow

8
1310 to 2s complement
  • Convert 1310 to binary using division algorithm
  • 11012 in 4-bit notation
  • As 4-bit notation causes overflow for positive
    numbers bigger than 7, represent 1310 in 8-bit
    notation.
  • 0000 11012
  • Stop here as this is a positive number.
  • Twos Complement of 1310 is

0000 11012
9
Decoding 2s Complement
  • First check the left-most bit
  • If left-most bit is 0, it represents a positive
    number
  • If left-most bit is 1, it represents a negative
    number
  • Now use the following method for decoding a
    negative number in 2s complement
  • Copy the given 2s complement bits from right to
    left until a 1 is copied
  • Then reverse all the remaining bits to the left
    all the way
  • Now convert this binary string to decimal using
    multiplication algorithm

10
Example Decoding 2s complement
  • Decode 1111 0011 in 2s complement to its decimal
    equivalent
  • First notice that left-most bit is a 1
  • Copy the string till a 1 is copied, and reverse
    the other bits to the left of the 1 just copied
  • 1111 0011
  • Copy up to first 1 1
  • Reverse the rest of the bits 0000 1101

11
Decoding negative number continued
  • 3. Convert this string (0000 1101) to decimal
    using multiplication algorithm
  • 0x27 0x26 0x25 0x24 1x23 1x22
    0x21 1x20
  • 0 0 0 0 8 4 0 1 1310
  • 4. We are not quite done yet. Whats missing?
    Notice that the left-most bit was a 1 in the
    original string representing 2s complement, so,
    this is a negative number. The answer therefore
    is

-1310
12
Decoding 2s complement positive numbers
  • Decode twos complement string 01111 to its
    decimal equivalent
  • First notice that left-most bit is a 0
  • this string represents a positive number
  • Decode using multiplication algorithm
  • 0x24 1x23 1x22 1x21 1x20
  • 0 8 4 2 1 1510
  • The answer therefore is

1510
13
Addition in 2s Complement
  • To add values represented in 2s complement
  • Use the same algorithm as for binary addition
  • Note that the resulting bit-pattern should be the
    same length as the problem, so, truncate the
    left-end bit appropriately
  • Check for overflow when adding two positive
    numbers or two negative numbers, by checking if
    the sign-bit of the result indicates correct
    arithmetic

14
Example Addition in 2s complement
  • Add the following numbers in 2s complement
  • 0101 0110
  • First, notice that both these numbers are
    positive, so the answer should be positive
  • Second, be aware that as this is addition of two
    positive numbers, there could be an overflow
    error
  • Perform binary addition algorithm as usual
  • 0101 510
  • 0110 610
  • 1011 -510
  • Notice that the result is the 2s complement of
    -510 !!
  • This indicates an overflow, because, 56 11,
    which is out of the range of 4-bit notation of
    2s complement.

15
Example Addition in 2s complement
  • Add the following numbers in 2s complement
  • 1010 1010
  • Again, notice that it is an addition of two
    negative numbers, so the result must be a
    negative number, and there is a possibility for
    overflow
  • Perform the usual binary addition
  • 1010 -6
  • 1010 -6
  • 10100 4
  • The result, after truncation to 4-bits,
    represents 410 !!
  • This indicates overflow as (-6) (-6) -12
    which cannot be represented in 4-bit 2s
    complement notation

16
Example Addition in 2s complement
  • Here is another example with overflow error add
    01110001 in 2s complement
  • Again, notice that they are both positive
    numbers, so, the result must be positive and,
    there could be an overflow error.
  • Perform the usual binary addition
  • 0111 7
  • 0001 1
  • 1000 8
  • Therefore the answer is 1000 in 4-bit 2s
    complement notation and there is an overflow
    error.

17
Addition in 2s complement NO Overflow
  • Here is an example with no overflow add 0100
    0011 in 2s complement
  • First, notice that it is an addition of two
    positive numbers, so the result must be a
    positive number, and there could be an overflow
  • Perform usual binary addition
  • 0100 0011 0111
  • The result 0111 represents 710 which can be
    represented in 4-bit 2s complement notation.
Write a Comment
User Comments (0)
About PowerShow.com