Surprising Boolean incompatibility

Discussions related specifically to the Windows, Linux (86), Mac OS-X and Raspberry Pi editions of BB4W and BBCSDL
Richard Russell
Posts: 714
Joined: Tue 18 Jun 2024, 09:32

Surprising Boolean incompatibility

Post by Richard Russell »

In programming languages which specify Boolean true to be the numeric value +1 you can usually safely assume that in practice any non-zero value will be accepted. This is because any code testing the Boolean will likely do so as follows:

Code: Select all

if (boolean) do_something()
It would be silly to do:

Code: Select all

if (boolean == TRUE) do_something()
not least because it will be slower.

But today I discovered that the box2dlib library was incompatible with running natively on an Apple Silicon Mac for this very reason! When putting the value -1 instead of +1 in a Boolean structure member (32-bit integer) it was actually being interpreted as false. :shock:

I've corrected the library at Github, and the MacOS Apple Silicon release has been updated accordingly, but I still find this extraordinarily surprising.