Tilde vs bang on a bool in C code

I accidentally use ~MyBool instead of !MyBool and spent all day truing to find my error.
Seems like for most compilers these would be the same. but not Arduino (at least not for GGC used with Pico)
From Help:
A bool holds one of two values, true or false . (Each bool variable occupies one byte of memory.)

// tilde check
bool myBool = true
Serial.print( "tilde check True: ");
Serial.print( myBool );
Serial.print( " tilde:");
Serial.print( ~myBool );
Serial.print( " bang!: ");
Serial.print( !myBool );

Results
tilde check True: 1 tilde:-2 bang!: 0
Who needs a -2 for a bool?
So simple if bool takes a byte anyway
why not internally use 255 (all ones) for true
and 0 for false?
Or check for ~ on a bool and fix it!

It looks like it's not. Apparently the modern standard calls for 1 = true, 0 = false.

Isn't the rule 0=false, and ANYTHING else = true? "false" and "true" are convenient for setting the value of a bool, but what matters most is how they are evaluated, and I believe what I wrote above is correct for evaluation. So a logical true vallue can be implemented as ANY non-zero value, and all should work properly regardless of the specific value used by the compiler.

The one that would have kept me busy was what happens when you

  bool xx = false;

  xx++;

// or

  bool yy = true;

  yy++;

and so forth. And I'm going to have to remember to see what, if any, warnings any of these get.

I get the point of having boolean variables, but (and?) I've seen straightforward and less straightforward use of good old zero and not zero. I think I went a few years using nothing but unsigned char for just about eveything.

I hate when that happens. :expressionless:

a7

Thanks for all the discussion.
Bottom line re ~myBool DON'T Do that!
So I guess my ask would be: for the compiler to know that a bool should not be bitwise negated since that won't work as expected.

The value is first promoted to int which is why Serial.print works as expected. There's no way to avoid that. The silent promotion is part of the language.

Inchestin'. THX.

I do not know how long ago I experimented with that kind of expression, but I do not remember it causing a compilation error.

I do not remember lotsa stuff, so.

Just now with the GDB online C++ compiler I only got a warning. Until I jumped the language setting to C++ 23.

a7

As I understand, in the arduino compiler, bools are stored as bytes.
When I was a kid, I was taught - as @RayLivingston mentioned, that false is ‘zero’ (all bits clear’), any non zero value is identified ‘true’