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: Philipp Burch
Subject: Re: [avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1
Date: Tue, 25 Mar 2008 17:17:09 +0100
User-agent: Thunderbird 2.0.0.12 (Windows/20080213)

Blake Leverett wrote:
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
Just (1UL << i) should work too.

Philipp

_______________________________________________
AVR-GCC-list mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list






reply via email to

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