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 andbitwise ORit with theresult, then store it intoresult. It is the equivalent of doing something like:result = result | callFunctionOne(sig);Taking your code example:
and your logic of
So that means that if you don’t define result, it will be by default FALSE.
callFunctionOnereturns TRUEresultequals TRUE.callFunctionOnereturns FALSEresult equals FALSE.
While it may seem that this is a
boolean OR, it still is using thebitwise ORwhich is essentiallyOR'ingthe number1or0.So given that
1is equal to TRUE and0is equal to FALSE, remember your truth tables: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.