avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


From: Blake Leverett
Subject: Re: [avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1
Date: Tue, 25 Mar 2008 09:04:11 -0600
User-agent: KMail/1.9.7

On Tuesday 25 March 2008, Moritz Federspiel wrote:
> Hello everyone!
> I got a little Problem on this simple Code:
>
> /-------------code start-------------------/
>
> uint32_t i; t;
>
> for(i=0; i<20; i++)
>   t = (1<<i);
>
> /-------------code stop-------------------/
>
> The results of this should be:
> 1
> 2
> 4
> 8
> 16
> 32
> 64
> 128
> 256
> 512
> 1024
> 2048
> 4096
> 8192
> 16384
> -->4294934528
>
> after the 16384 i get 4294934528, I dont know where the problem is.
>

The problem is that (1<<i) assumes that '1' is an integer, or 16 bits.  And 
it's signed, so when it shifts 15 times, you get -32768 (or so), and that 
converts to an unsigned long as the big number you see.

((uint32_t)1 << i) (or something like that) should work.

Blake




reply via email to

[Prev in Thread] Current Thread [Next in Thread]