Thread: Why 1s complement is -2

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Question Why 1s complement is -2

    Code:
    int main()
    {
           int a=1,b;
           b=~1;
           printf(""%d",b);
           return 0;
    }
    pls explain by showing bitwise operation it will be helpful to understand...

    thanks in advance.......

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please do your own homework. You can easily find a rather direct answer to this by searching the Web.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Think it as binary:
    Code:
    00000000000000000000000000000001 // 1
    What does NOT do:
    Code:
    11111111111111111111111111111110 // -2
    Since you are declaring it as signed the compiler treats it are a little negative instead of a large positive.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think of the sign bit, only:

    The sign bit can be a one or a zero. If the sign bit isn't a one, then it must be a zero, and vice-versa.

    What else could a complement of a positive number be, since you've changed the sign bit?

    Now apply that to all the bits in the binary for 1.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Thanks

    many many thanks to Sipher & Adak for their valuable time and lucid explanation...........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1's And 2's Compliemnt
    By sangood in forum C Programming
    Replies: 4
    Last Post: 10-13-2009, 02:10 AM
  2. counting 1's in a binary byte
    By muran_pling in forum C++ Programming
    Replies: 23
    Last Post: 08-24-2006, 05:45 PM
  3. Counting 1's in a string of 1's and 0's
    By Friar in forum C Programming
    Replies: 1
    Last Post: 02-21-2006, 09:50 AM
  4. two's complement question
    By NetWeirdo in forum C Programming
    Replies: 1
    Last Post: 12-10-2005, 02:36 PM
  5. 1's and 2's complement
    By Jaguar in forum C Programming
    Replies: 3
    Last Post: 10-14-2003, 09:12 AM