mercredi 7 janvier 2015

Is it always safe to shift bool values?


I stumbled about a Cppcheck warning (an inconclusive one), that I mistakenly used & instead of &&:



int encodeCmdState(bool finished, short param, bool err)
{
return
(finished & 1) |
((param & 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?





Aucun commentaire:

Enregistrer un commentaire