Search code examples
c++tilde

Why does ~x on an int result in -(x+1)


Why does this code:

#include <iostream>
int main ()
{
  int x = 1;
  int y = ~x;
  std::cout << y;
}

Always print -(x+1)? If x = 00000001, shoudn't y = 11111110?


Solution

  • That's because you're on a two's complement system. C++ doesn't guaratee that, but all (citation needed?) modern architectures have this property.