C++ Logo

sg12

Advanced search

Re: [ub] Signed shifting

From: Howard Hinnant <howard.hinnant_at_[hidden]>
Date: Mon, 17 Nov 2014 20:44:26 -0500
On Nov 17, 2014, at 8:04 PM, Samuel Neves <sneves_at_[hidden]> wrote:

> On 17-11-2014 23:14, Howard Hinnant wrote:
>> Examples of real-world code that want to do arithmetic right shifts on both positive and negative values. This code is portable across all two’s complement machines, assuming that the compiler does not take advantage of the current ub.
>
> For what it's worth, there is currently no UB in those examples; signed right shift is implementation-defined, as
> mentioned upthread.

Thank you much Samuel. I am guilty of not reading the spec before I posted. Mea-culpa. And this is very good news! :-)

<snip>

> The situation is worse with signed left shifts, which actually invoke undefined (as opposed to implementation-defined)
> behavior. C++14 (DR1457) already partially fixed the signed left shift situation by allowing shifts into (but not over)
> the sign bit--this allows to define INT_MIN as, e.g., 1 << 31 without running into UB. I don't see why this relaxation
> cannot be extended to shifts that go beyond the sign bit.

100% agreed. This should be implementation defined, same as the right shift. Thank you for shining light on this.

Howard

Received on 2014-11-18 02:44:33