On 2009-10-02 17:43,
<> wrote:
> On Thu, 1 Oct 2009 13:22:10 -0700, "easily_confused" <> wrote:
>>On a 32 bit machine, ~0<<32 gives 11111111111111111111111111111111 (I was
>>expecting 0)
>>
>>(~0<<31)<<1 gives 0.
>>
> And this is correct.
>
> I don't understand what you mean.
>
> ~0 << 0 (or just ~0) gives
> 11111111111111111111111111111111
>
> ~0<<32 gives
> 11111111111111111111111111111111
>
> 32 bit shifts later you get the same thing.
> Which makes sence.
Mathematically it doesn't make sense.
(x << y) << z should be same as x << (y + z)
However, from the point of view of a processor designer it does make
sense.
> I'm pretty sure that you only get 0-31 bit shifts
> on 32 bit systems.
>
> So that:
> << 32 equals << 0
> << 33 equals << 1
> << 34 equals << 2
> ..
> ..
> << 31 equals << 31
[...]
> This is just my guess. I imagine the pre-processor
> rolls 32 back to 0.
Actually, it's the processor. x86 type CPUs (since the 386 or 486, IIRC)
use a so-called barrel shifter to shift by arbitrary amounts in a single
cycle. To keep that shifter at a reasonable size (by 1980's standards of
processor design) it uses only the lower 5 bits of shift width and
ignores the rest.
hp