I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly use
ID: 659058 • Letter: I
Question
I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:
/// @param code identifies the command. Only the lower 16 bits of are being processed
int encodeCmdState(bool finished, int code, bool errorneous)
{
return
(finished & 1) |
((code & 0xffff)<<1) |
((err & 1)<< 17);
}
I'm not sure if this code (style) reaches back into times where bool wasn't available in C. My first idea would be to simply strip the & 1 normalization from the bool parts. But I want to be sure, so...
Is shifting bool values "portable" and "safe"? Or, more generally:
Does the implicit bool-to-int conversation in arithmetic expressions always normalize values?
Explanation / Answer
According to the C++ standard,