On 19 Jan, David Chisnall wrote: > On 19 Jan 2018, at 05:07, Conrad Meyer <c...@freebsd.org> wrote: >> >> The spec says the behavior is undefined; not that the compiler has to >> produce a warning or error message. The compiler *does* get to >> arbitrarily decide what it wants to do when it encounters UB. It is >> wholly free to implement this particular UB with the logical result >> and no warning/error. > > First, you are not correct that the only logical outcome of a shift of > greater than the width of a type is 0. In C, a right-shift of a > signed type propagates the sign bit. Right shifting a negative 32-bit > int by 16 and then again by 16 is not undefined behaviour (though > doing the shift as a single operation is) and will give you a value of > -1.
Propagating the sign when doing a right shift is the common behaviour, but I believe this is actually implemenation defined. If the machine doesn't have arithmetic shift instructions, then a logical shift which fills in zeros on the left is also conformant. See the edit to the first answer here: https://stackoverflow.com/questions/1857928/right-shifting-negative-numbers-in-c I think powerpc falls into this category. _______________________________________________ svn-src-head@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-head To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"