I heard that often "everything" besides 0 is true. But now very strange things are happening to me... or I just think that I do it in correct way while I don't. Here's what is happening:
When I want to check if a is equivalent b, I can use NOT(a XOR b)
. When I checked it for unsigned char
's, everything was ok, for example
unsigned char a = 5;
unsigned char b = 3;
unsigned char c = ~(a^b);
gave me c == 249
:
a is: 00000101
, which is 5.
b is: 00000011
, which is 3.
~(a^b) is: 11111001
, which is 249.
Now, let's try this with bool
's.
cout << ~(true^true) << ~(true^false) << ~(false^true) << ~(false^false) << endl;
cout << ~(~(true^true)) << ~(~(true^false)) << ~(~(false^true)) << ~(~(false^false)) << endl;
if (~(true^true) == true)
cout << "true";
else
cout << "false";
This gives me in console:
-1-2-2-1
0110
false
while I expected the first line to be:
1001
After asking a friend, he advised me to try !
instead of ~
and see if it will work correctly. And (I think) it works correctly now. But I don't understand why. Shouldn't boolean negation work for bools?
bool
has more than one bit. ~ flips each bit. – Interventionisttrue
. – Wee-1
? Isn't0
orfalse
represented in this case as00000000
? – Coactive