Unary NOT on 32-bit integers

This is a discussion on Unary NOT on 32-bit integers within the C Programming forums, part of the General Programming Boards category; How come performing the unary NOT (~) opertion on int x = 0 gives the value of -1 and not ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    Unary NOT on 32-bit integers

    How come performing the unary NOT (~) opertion on int x = 0 gives the value of -1 and not a saturated int value?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because of two's complement representation.

    (x) + (~x +1) must always equal 0.

    0 + -1 + 1 = 0

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    Even with unsigned integers?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're just flipping all the bits, so the result can never be saturated. In a one's complement system the result would be 0 (-0) (signed).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    How were you expecting negative numbers to be represented in an int?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    Yea it makes sense to me for signed integers, but for unsigned integers I am not clear why this is also the case. Guess I should brush up on two's complement.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    OK I think I see that C does not allow unsigned integers of this magnitude, but the bit representation is still all ones, correct? That's all that really matters to me.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    For unsigned number you should get INT_MAX.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    #include <stdio.h>
    
    int main() {
            unsigned int x = 0;
            printf("%u\n", ~x);
    }

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    %u, very helpful, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. 32 bit color depth bitmap problem with XP
    By kalabala in forum C++ Programming
    Replies: 0
    Last Post: 12-22-2008, 05:56 AM
  3. c++ bitwise operators with 64 bit integers? possible?
    By chrisd1100 in forum C++ Programming
    Replies: 8
    Last Post: 09-04-2007, 07:13 PM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 07:20 AM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM

Tags for this Thread


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