I have a question about "|=" in c++, how this operator works, for example:
bool result;
result |= callFunctionOne(sig);
result |= callFunctionTwo(sig);
result |= callFunctionThree(sig);
result |= callFunctionFour(sig);
and the function called above, will reutrn "true" if the paramater sig is processed in the function, otherwish, return "false";
the sig can be processed only in one function each time, how the "|=
" works?
|
is bitwise OR.
|=
says take what is returned in one of your function and bitwise OR
it with the result
, then store it into result
. It is the equivalent of doing something like:
result = result | callFunctionOne(sig);
Taking your code example:
bool result;
result |= callFunctionOne(sig);
result |= callFunctionTwo(sig);
result |= callFunctionThree(sig);
result |= callFunctionFour(sig);
and your logic of
will reutrn "true" if the paramater sig is processed in the function, otherwish, return "false";
So that means that if you don't define result, it will be by default FALSE.
result = false;
callFunctionOne
returns TRUE
result = result | callFunctionOne;
result
equals TRUE.
result = false;
callFunctionOne
returns FALSE
result = result | callFunctionOne
result equals FALSE.
While it may seem that this is a boolean OR
, it still is using the bitwise OR
which is essentially OR'ing
the number 1
or 0
.
So given that 1
is equal to TRUE and 0
is equal to FALSE, remember your truth tables:
p q p ∨ q
T T T
T F T
F T T
F F F
Now, since you call each function after another, that means the result of a previous function will ultimately determine the final result from callFunctionFour
. In that, three-quarters of the time, it will be TRUE and one-quarter of the time, it will be FALSE.