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