I’ve been struggling for a while with a part of my code and I finally found that the problem lies with a simple test that don’t give me the result I expect.
if (2) //=> true
if (2 & true) //=> false
if (bool(2) & true) //=> true
What I don’t understand is why the second line results in false.
My understanding was that every non-zero integer was considered as true in a test.
Because the bitwise and between
2andtrueis false.&(bitwise operator) is different than&&(logical operator).truecast tointis1.So
2 & trueis2 & 1which is false – because0000000000000010 & 0000000000000001 == 0. (bits may vary)Whereas
bool(2) == 1, and1 & 1istrue.