top | item 46944645

(no title)

pwagland | 21 days ago

And, indeed, if you look at the author's writeup, you see exactly that the generated code satisifes `(rotate == true || rotate == false) == false`, since rotate is checked explicitly against 0 and 1. The essence of the difference is:

> When boolean is an enum, the compiler does exactly what I expected – the == false condition checks if the value is equal to 0, and the == true condition checks if the value is equal to 1.

> However, when boolean is actually _Bool, the == false check is transformed into != 1, and the == true check is transformed into != 0 – which makes perfect sense in the realm of boolean logic. But it also means that for a value of 255, hilarity ensures: since 255 is neither 0 nor 1, both conditions pass!

So a value of 255 also makes both checks fail for the enum, but because of the ordering of the code, it was expected to evaluate as != false.

Had the check: ``` if (sprtemp[frame].rotate == false) ```

been written as: ``` if (sprtemp[frame].rotate != true) ```

then it would work for the `bool` type, but not the `enum` type, at least in C23 mode. Assumedly the C++ mode (effectively) treated the boolean as an enum, or possibly as `false == 0`, `true != 0`.

discuss

order

No comments yet.