Reversing bits

Hi,

I'm trying to take in user input (which should be an integer), convert that to binary, flip the binaries and then print the decimal version of the flipped binaries.

When I test it with 4294967295 (which is the maximum number for 32-bit) it SHOULD return 0 once it's all flipped. I'm getting 2147483648 instead. Anything wrong with my program?

1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std;

int main() {
  int count;
  cin >> count; //expecting user input to be of type int
  int *array = new int[count];
  for (int i = 1; i <= count; i++) {
    cin >> array[i]; //user input at index 0, 1, 2, etc...
    bitset<32> myBits (array[i]); //the binary version of the decimal at index 0, 1, 2, etc...
    bitset<32> invertedBits = ~myBits; //flip all the bits
    cout << invertedBits.to_ulong() << endl; //print the decimal version of the bitsets
  }
}

When I test it with 4294967295 (which is the maximum number for 32-bit)

2,147,483,647 is the maximum value for a 32-bit signed integer, which is the type you're storing the value in on line 8.
You are my hero. Thank you. God bless.
Topic archived. No new replies allowed.