lundi 17 décembre 2018

Is (int32_t) 255 << 24 undefined behavior in gcc (C++11)?

In C++11, according to en.cppreference.com,

For signed and non-negative a, the value of a << b is a * 2b if it is representable in the return type, otherwise the behavior is undefined.

My understanding is that, since 255 * 224 is not representable as an int32_t, the evaluation of (int32_t) 255 << 24 yields undefined behavior. Is that correct? Can this be compiler-dependent? It's an IP16 environment, if that matters.

Background: this comes from an argument I am having with a user at arduino.stackexchange.com. According to him, “there's nothing undefined about that at all”:

you notice that much of the bit shifting is "implementation defined". So you cannot quote chapter-and-verse from the specs. You have to go to the GCC documentation since that is the only place that can tell you what actually happens. gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Shifting - it's only "undefined" for a negative shift value.

Aucun commentaire:

Enregistrer un commentaire