Hi Ranier,int main ()
On 8/3/20 4:59 PM, Ranier Vilela wrote:
> Em seg., 3 de ago. de 2020 às 10:53, Gregory Burd <greg@burd.me> escreveu:
>
>> Sjoerd,
>>
>> Nice explanation, so if they are both correct and one avoids unnecessary
>> work (even just a tiny bit) why not adjust the code to add your explanation
>> as a comment and use the more efficient version?
>>
> I'm not sure that is correct.
> It is the equivalent of writing:
> if (b->key)
To see that the code is correct, you can write down the truth table of the *bitwise* AND operator:
| b->ttype != TYPE_void | b->tkey || (b->ttype != TYPE_void) & b->tkey |
|-----------------------+--------------++-----------------------------------|
| false (== 0) | false (== 0) || 0 (== false) |
| true (== 1) | false (== 0) || 0 (== false) |
| false (== 0) | true (== 1) || 0 (== false) |
| true (== 1) | true (== 1) || 1 (== true) |
With a little thought you can see that this is the same as the *logical* AND operator. It works because both operands are of type bool, so their values are 0 for false and 1 for true in C99 if you cast them to integral types. (see for example the answers to this Stack Overflow question: https://stackoverflow.com/questions/4767923/c99-boolean-data-type)
As Sjoerd wrote in his email the only difference might be a performance penalty in case the first operand is true, if the second operand is expensive to evaluate. This is not the case here because b->tkey is a bool value so its evaluation is cheap.
In any case, if you find a particular set of inputs where this (or any other) part of the code produces wrong results we would be interested to see a bug report.