1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

1's and 2's complements of binary numbers

Discussion in 'Mathematics and Physics' started by PG1995, Oct 10, 2011.

  1. PG1995

    PG1995 Active Member

    Joined:
    Apr 18, 2011
    Messages:
    1,645
    Likes:
    13
    Hi

    I request you to keep you replies as simple as possible and keep everything simple. Thanks.

    Most computers (I'm not sure if all) do most of the arithmetic using addition operation - i.e. subtraction, multiplication, and division all use addition operation. This makes circuit design simple and with only one circuit you get to do different things.

    What I guess 1's and 2's complements let us do all operations, subtraction, division, and multiplication using addition method.

    complement (noun)
    2 c : a number that when added to another number of the same sign yields zero if the significant digit farthest to the left is discarded —used especially in assembly language programming
    [M-W's Col. Dic.]


    In base 10 the compliment of 3 would be 7 because 3+7 = 10 and after discarding the 1, we are left with 0

    Thus 4 is the compliment of 6, and 8 of 2, etc.

    The compliment of 55 would be 45, because 55+45=100 and removing the 1 leaves you with 0 again.

    With binary, the compliment of 1 is 1 because 1+1 =10

    The compliment of 1010 would be 0110 (which is 2's complement of 1010) because 1010 + 0110 = 10000 in binary.

    1: Let's say we have a binary number 110 (6). It's 1's complement would be: 001 (1), and 2's complement would be: 010 (2). I don't see what 1's and 2's complements of the number tells us. Please help me with it.

    2: Perhaps, using a particular example could help us a bit. We have binary number 1010 (10) and we want to subtract 110 (6) from it - i.e. 1010 - 110. In base 10 arithmetic it would be 10 - 6 = 4. So, how do complements help us here?

    1's complement of 110 = 001 (1)
    2's complement of 110 = 010 (2)

    I do have more related queries on this topic but I would ask them after clearing this up. Thanks.

    Scanned pages which can be useful:
    1: http://img510.imageshack.us/img510/2465/floyddigitalfundamental.jpg
    2: http://img836.imageshack.us/img836/2465/floyddigitalfundamental.jpg
     
    Last edited: Oct 10, 2011
  2. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    Binary compliments are primarily used for things like bit manipulation, or manipulation of negative numbers in binary.

    The function of 1's compliment is very very simple, invert every bit in a register, it should be used whenever you need to invert bits not just for arithmetic operations, otherwise typically without the one's compliment instruction you'd have to use a separate register and OR it in order to invert and it may take two or three instructions where one's compliment is often an atomic instruction and requires no additional supplied constants or registers.

    The two's compliment is often used to manipulation of negative numbers.
    The Wikipedia entries for
    One's compliment
    and
    Two's compliment
    provide a wealth of information.

    One's compliment is basically an ASM shortcut for inversion which is an atomic instruction and is used quiet a bit to reduce resources where it's practical. The Two's compliment is more directly related to arithmetic, thought the combination of the two can lead to yet more ASM efficiency gains in program execution/storage depending on how well optimized the code is to use these shortcut instructions when they're most practical.
     
    Last edited: Oct 10, 2011
  3. Ratchit

    Ratchit Well-Known Member

    Joined:
    Mar 12, 2008
    Messages:
    1,934
    Likes:
    78
    PG1995,

    Nope, it would take to long to do multiplication/division by addition/subtraction. Complements are useful for doing subtraction by addition.

    Notice that "complement" is spelled with a "e", not an "i". No need to get sloppy. Generally a complement is a quantity needed to make a thing complete. In the above case, 7 has to be added to 3 to make complete the number radix 10 decimal.

    I think some examples are in order. We usually use the radix 10 (decimal) and the radix 2 (binary), but let us chose some other radix like 3. What is the 3's complement of 120 radix 3? Since 120 has 3 digits, we subtract it from 3^3 or 27 decimal or 1000 base 3.
    Code (text):

     1000
    -0120
    ------
      110 = 12 decimal
     
    Or we can do it with decimal numbers.
    Code (text):

     27
    -15
    -----
     12
     
    Now, let's find the diminished radix complement or 2's complement of the same number, 120 radix 3. So we subtract 120 from 3^3-1 or 222 base 3.

    Code (text):

     222
    -120
    -----
     102 = 11 decimal
     
    Or we can do it with decimal numbers.

    Code (text):

     26
    -15
    -----
     11
     
    Ratch
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. KeepItSimpleStupid

    KeepItSimpleStupid Well-Known Member Most Helpful Member

    Joined:
    Oct 30, 2010
    Messages:
    9,917
    Likes:
    1,097
    ONLINE

    PG:

    I think your missing a concept and that is the MSB CAN be used as a sign bit and is for the two's complement form, otherwise you get two representaions of zero (all zeros and all ones). So, in the two's complement 16 bit form, integers range from -32768 to +32767

    What's interesting is that a byte can also have a 2's complement form and have values of -128 to +127. Now the cool part is something called "sign extension".
    If you replicate the Most Significant Bit of the 8 bit byte all the way out to the MSB of the 16 bit word, you have the 2's complement 16 bit representation
    of the number.
     

Share This Page