complement ~

This is a discussion on complement ~ within the C Programming forums, part of the General Programming Boards category; How on earth can Code: int z = -1; printf("%d\n", ~z); give 0? Shouldnt z get printed as INT_MAX - ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    complement ~

    How on earth can

    Code:
    int z = -1;
    printf("%d\n", ~z);
    give 0? Shouldnt z get printed as INT_MAX - 1?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Most architectures use 2's complement for signed integers, and in that system -1 is represented by all bits on.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Oh. So the bigest integral negative number (INT_MIN) is represented as all zeroes?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All zeroes is 0. A one, followed by 15/31/63 zeroes, is the most negative number in a 2's complement system.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Tool View Post
    Oh. So the bigest integral negative number (INT_MIN) is represented as all zeroes?
    Yep! all zeros except for the MSB(it).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and 32 bit two's complement
    By The Urchin in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 02:17 AM
  2. two's complement question
    By NetWeirdo in forum C Programming
    Replies: 1
    Last Post: 12-10-2005, 01:36 PM
  3. how to complement
    By stautze in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 06:16 AM
  4. Two's Complement
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 02-26-2002, 04:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21