Re: Why is UInt32 << 32 a no-op?
Re: Why is UInt32 << 32 a no-op?
- Subject: Re: Why is UInt32 << 32 a no-op?
- From: Jeremy Pereira <email@hidden>
- Date: Tue, 17 Nov 2009 17:15:16 +0000
On 17 Nov 2009, at 16:56, Jens Alfke wrote:
> Surprisingly, the expression "n << 32", where n is a 32-bit integer, is a no-op. I would expect it to result in zero for all values of n.
>
> UInt32 n = (UInt32)-1L;
> int shift = 32;
> n = n << shift;
> assert(n==0); // fails; n is actually unchanged
>
> This is breaking some code of mine that masks out the upper b bits of a number, where 0≤b≤8; it fails when b==0 because the mask it generates is all 1s instead of all 0s. I'm going to have to add a special case.
>
> Any C expert know whether this is in-spec or not?
Not an expert, but from the C99 standard (6.5.7):
"If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined."
I speculate that the reasoning behind that statement is that some hypothetical architecture might choose to represent a shift of a 32 bit number using a 5 bit field to specify the number of bits to shift.
>
> —Jens _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden