mercredi 18 mars 2015

Undefined behaviour of right shift (a >> b) when b is greater than the number of bits in a?


Apparently, the behaviour of the right shift operation:



a >> b


is undefined in C and C++ when b >= sizeof(a)*CHAR_BIT (whereas in the normal case, the "new bits" introduced from the left due to the right shift are equal to zero).


Why is this undefined behaviour better than setting the result to zero when b >= sizeof(a)*CHAR_BIT?




Aucun commentaire:

Enregistrer un commentaire