C++ Logo

sg12

Advanced search

Re: [ub] ub due to left operand of shift

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Mon, 28 Oct 2013 07:45:18 +0100
On 10/28/2013 05:31 AM, Lawrence Crowl wrote:
> The original definition fails for the earlier example.
>
> int adjust( unsigned char E1 ) { return E1 << 24; }
>
> E1 is unsigned, and therefore the text says to compute mod INT_MAX rather
> than UINT_MAX. This behavior is clearly not what anyone is doing.

Here's 5.8p2. E1 and E2 are assumed to have their promotions already applied.
(I believe this is standard phrasing, but could be made clearer.)

The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned
type, the value of the result is E1 × 2^E2 , reduced modulo one more than the maximum value representable
in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2^E2 is representable
in the corresponding unsigned type of the result type, then that value, converted to the result type, is the
resulting value; otherwise, the behavior is undefined.

Why does your example from above fail?

Thanks,
Jens

Received on 2013-10-28 07:45:34