6 ms·
>But I can't understand why e.g. use of an oversized shift amounts are UB Suppose a CPU, when shifting a 32 bit register, uses only the lower 5 bits of the shi
by Kristine1975 11y ago
>But I can't understand why e.g. use of an oversized shift amounts are UB
Suppose a CPU, when shifting a 32 bit register, uses only the lower 5 bits of the shift count. Then shifting a register by 32 will be a shift by 0, i.e. a no-op (instead of clearing the register). The compiler would have to generate additional code to correctly handle this which would be slower. So it was decided to make oversized shifts UB.
- tomp 11y ago> So it was decided to make oversized shifts UB. Exactly the stupidity I was pointing out - why make it undefined behaviour, when you could make it implementation-defined behaviour. You might argue the effects are the same, but they aren't, of course - the first is global (the whole program is invalid), whereas the second is local (only that instruction has an unknown (but probably still deterministic) outcome, and only in specific circumstances).