I was just tinkering with use of bitwise operators on bool data type, so I wrote a simple program.
#include <iostream>
int main()
{
bool b = 1;
std::cout<<~b;
return 0;
}
This code, to my surprise, gave me the output -2
.
~
-operator flips all the bits on variable its used on and bool only holds 2 distinct values and is made of 8 bits or 1 byte.
So 0b000 is interpreted as 0 that is false and 0b111 is interpreted as 1 that is true.
So value 1 will be 0b111 and using bit-wise not(~) operator on it will flip all bits to 0b000 that is 0.