What is “2's Complement”?

前端 未结 23 2335
名媛妹妹
名媛妹妹 2020-11-21 05:59

I\'m in a computer systems course and have been struggling, in part, with Two\'s Complement. I want to understand it but everything I\'ve read hasn\'t brought the p

相关标签:
23条回答
  • 2020-11-21 06:12

    2’s Complements: When we add an extra one with the 1’s complements of a number we will get the 2’s complements. For example: 100101 it’s 1’s complement is 011010 and 2’s complement is 011010+1 = 011011 (By adding one with 1's complement) For more information this article explain it graphically.

    0 讨论(0)
  • 2020-11-21 06:15

    I liked lavinio's answer, but shifting bits adds some complexity. Often there's a choice of moving bits while respecting the sign bit or while not respecting the sign bit. This is the choice between treating the numbers as signed (-8 to 7 for a nibble, -128 to 127 for bytes) or full-range unsigned numbers (0 to 15 for nibbles, 0 to 255 for bytes).

    0 讨论(0)
  • 2020-11-21 06:16

    REFERENCE: https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html

    I invert all the bits and add 1. Programmatically:

      // in C++11
      int _powers[] = {
          1,
          2,
          4,
          8,
          16,
          32,
          64,
          128
      };
    
      int value=3;
      int n_bits=4;
      int twos_complement = (value ^ ( _powers[n_bits]-1)) + 1;
    
    0 讨论(0)
  • 2020-11-21 06:17

    The word complement derives from completeness. In the decimal world the numerals 0 through 9 provide a complement (complete set) of numerals or numeric symbols to express all decimal numbers. In the binary world the numerals 0 and 1 provide a complement of numerals to express all binary numbers. In fact The symbols 0 and 1 must be used to represent everything (text, images, etc) as well as positive (0) and negative (1). In our world the blank space to the left of number is considered as zero:

                      35=035=000000035.
    

    In a computer storage location there is no blank space. All bits (binary digits) must be either 0 or 1. To efficiently use memory numbers may be stored as 8 bit, 16 bit, 32 bit, 64 bit, 128 bit representations. When a number that is stored as an 8 bit number is transferred to a 16 bit location the sign and magnitude (absolute value) must remain the same. Both 1's complement and 2's complement representations facilitate this. As a noun: Both 1's complement and 2's complement are binary representations of signed quantities where the most significant bit (the one on the left) is the sign bit. 0 is for positive and 1 is for negative. 2s complement does not mean negative. It means a signed quantity. As in decimal the magnitude is represented as the positive quantity. The structure uses sign extension to preserve the quantity when promoting to a register [] with more bits:

           [0101]=[00101]=[00000000000101]=5 (base 10)
           [1011]=[11011]=[11111111111011]=-5(base 10)
    

    As a verb: 2's complement means to negate. It does not mean make negative. It means if negative make positive; if positive make negative. The magnitude is the absolute value:

            if a >= 0 then |a| = a
            if a < 0 then |a| = -a = 2scomplement of a
    

    This ability allows efficient binary subtraction using negate then add. a - b = a + (-b)

    The official way to take the 1's complement is for each digit subtract its value from 1.

            1'scomp(0101) = 1010.
    

    This is the same as flipping or inverting each bit individually. This results in a negative zero which is not well loved so adding one to te 1's complement gets rid of the problem. To negate or take the 2s complement first take the 1s complement then add 1.

            Example 1                             Example 2
             0101  --original number              1101
             1's comp  1010                       0010
             add 1     0001                       0001
             2's comp  1011  --negated number     0011
    

    In the examples the negation works as well with sign extended numbers.

    Adding:
    1110 Carry 111110 Carry 0110 is the same as 000110 1111 111111 sum 0101 sum 000101

    SUbtracting:

        1110  Carry                      00000   Carry
         0110          is the same as     00110
        -0111                            +11001
      ----------                        ----------
    sum  0101                       sum   11111
    

    Notice that when working with 2's complement, blank space to the left of the number is filled with zeros for positive numbers butis filled with ones for negative numbers. The carry is always added and must be either a 1 or 0.

    Cheers

    0 讨论(0)
  • 2020-11-21 06:17

    It is a clever means of encoding negative integers in such a way that approximately half of the combination of bits of a data type are reserved for negative integers, and the addition of most of the negative integers with their corresponding positive integers results in a carry overflow that leaves the result to be binary zero.

    So, in 2's complement if one is 0x0001 then -1 is 0x1111, because that will result in a combined sum of 0x0000 (with an overflow of 1).

    0 讨论(0)
  • 2020-11-21 06:22

    I wonder if it could be explained any better than the Wikipedia article.

    The basic problem that you are trying to solve with two's complement representation is the problem of storing negative integers.

    First consider an unsigned integer stored in 4 bits. You can have the following

    0000 = 0
    0001 = 1
    0010 = 2
    ...
    1111 = 15
    

    These are unsigned because there is no indication of whether they are negative or positive.

    Sign Magnitude and Excess Notation

    To store negative numbers you can try a number of things. First, you can use sign magnitude notation which assigns the first bit as a sign bit to represent +/- and the remaining bits to represent the magnitude. So using 4 bits again and assuming that 1 means - and 0 means + then you have

    0000 = +0
    0001 = +1
    0010 = +2
    ...
    1000 = -0
    1001 = -1
    1111 = -7
    

    So, you see the problem there? We have positive and negative 0. The bigger problem is adding and subtracting binary numbers. The circuits to add and subtract using sign magnitude will be very complex.

    What is

    0010
    1001 +
    ----
    

    ?

    Another system is excess notation. You can store negative numbers, you get rid of the two zeros problem but addition and subtraction remains difficult.

    So along comes two's complement. Now you can store positive and negative integers and perform arithmetic with relative ease. There are a number of methods to convert a number into two's complement. Here's one.

    Convert Decimal to Two's Complement

    1. Convert the number to binary (ignore the sign for now) e.g. 5 is 0101 and -5 is 0101

    2. If the number is a positive number then you are done. e.g. 5 is 0101 in binary using twos complement notation.

    3. If the number is negative then

      3.1 find the complement (invert 0's and 1's) e.g. -5 is 0101 so finding the complement is 1010

      3.2 Add 1 to the complement 1010 + 1 = 1011. Therefore, -5 in two's complement is 1011.

    So, what if you wanted to do 2 + (-3) in binary? 2 + (-3) is -1. What would you have to do if you were using sign magnitude to add these numbers? 0010 + 1101 = ?

    Using two's complement consider how easy it would be.

     2  =  0010
     -3 =  1101 +
     -------------
     -1 =  1111
    

    Converting Two's Complement to Decimal

    Converting 1111 to decimal:

    1. The number starts with 1, so it's negative, so we find the complement of 1111, which is 0000.

    2. Add 1 to 0000, and we obtain 0001.

    3. Convert 0001 to decimal, which is 1.

    4. Apply the sign = -1.

    Tada!

    0 讨论(0)
提交回复
热议问题