printing a boolean value in c++
c++
Solution
`~` performs bitwise negation. The operand is promoted (in this case) to `int`, and all the bits are inverted. 1 has a binary representation of `00....001`, so this gives the binary value `11....110`, which is interpreted (on most modern computers) as -2.
Use `!` for logical negation.
Problem
When I run this simple code, ``` int main(int argc, const char * argv[]) { bool digit(true); std::cout << digit << " " << ~digit << std::endl; } ``` The output is ``` 1 -2 ``` I was expecting 1 and 0 (for true and false). Am I missing something here?