Why not use: x = !x;?

Regardless of how bool is represented underneath the hood, any non-zero value is converted to true when it's converted to a bool, so no worries about that happening.

There was a time when Arduino headers mistakenly typedefed (or #defined) bool as byte (edit: it might have been the Arduino-specific boolean type, not bool), and that did have exactly the problem that you described. See also BOOL / bool / Boolean / NSCFBoolean - NSHipster for a similar case of this.

1 Like