Suppose I have two functions which may have side effects, and return boolean values. (with bool
as defined in <stdbool.h>
, so that defines bool
as the _Bool
type)
bool tweedledee(MyState *pmystate);
bool tweedledum(MyState *pmystate);
Is it safe to use bitwise operators to combine them?
bool both_tweedles = tweedledee(&mystate) & tweedledum(&mystate);
or
bool either_tweedle = tweedledee(&mystate) | tweedledum(&mystate);
I would traditionally use the logical operators &&
or ||
; I'm working on a project where my team members are using the bitwise operators, since the functions may have side effects, and we want both function calls to occur. (The logical operators are short-circuiting.)
My only reservation is that I'm not sure whether a function returning bool
can safely be assumed to return 1 and 0, rather than something else as an alternate value for true
.
Just as an example: what if someone evil implemented them as follows?
bool tweedledee(MyState *pmystate)
{
return 66;
}
bool tweedledum(MyState *pmystate)
{
return 33;
}
then 66 & 33
is 0, and 66 | 33
is 99.