# zero shift

Discussion in 'C++' started by Mike - EMAIL IGNORED, Mar 30, 2007.

1. ### Mike - EMAIL IGNOREDGuest

Given:
unsigned x = 123;
unsigned y = 0;
unsigned a = x << y;
unsigned b = x >> y;

I would hope that: a == 123 && b == 123;

but I don't see this in section 5.8 of ISO/IEC 14882.
Is it true? Chapter & verse?

Thanks,
Mike.

Mike - EMAIL IGNORED, Mar 30, 2007

2. ### Kai-Uwe BuxGuest

Mike - EMAIL IGNORED wrote:

> Given:
> unsigned x = 123;
> unsigned y = 0;
> unsigned a = x << y;
> unsigned b = x >> y;
>
> I would hope that: a == 123 && b == 123;
>
> but I don't see this in section 5.8 of ISO/IEC 14882.
> Is it true? Chapter & verse?

I think[5.8] covers that case: the right operand is neither negative nor
greater than or equal to the length of the promoted left operand. Thus,
[5.8/1] does not say it's undefined.

For x << y, now [5.8/2] kicks in. We are told that the result is x * 2^y.
Since 2^0 = 1, we find a = x = 123.

For x >> y, [5.8/3] says b = x / ( 2^y) = x /1 = 123.

Best

Kai-Uwe Bux

Kai-Uwe Bux, Mar 30, 2007