Hi David,
Even on a 16-bit processor you can still do 32-bit operations (the
compiler will insert multiple instructions). I recently had similar
problems when doing 32-bit calculations. I don't know how to properly
solve this issue.
Use the L modifier to indicate to the compiler that the numeric constant
is a long, ie: uint32_t b = 64000L;
I don't know how the printf function is implemented. I could imagine
that by default it looks for a 16-bit value when you indicate %d, plus
it would assume that b is signed. So the value in parantheses would then
actually be the other 16 bits taken from b, rather than from sizeof(b).
You could use %u instead to make sure printf knows the argument is
unsigned. On a traditional system there is also the possibility to
indicate to printf that you're dealing with long numbers, something like
printf("b: %lu (%d)\n", b, sizeof(b));.
I would be interested in learning whether this solved your issues.
Cheers,
Urs
David wrote:
> Hi. I'm having problems with some unsigned 32-bit variables (on a
> msp430, tmote sky, tinyos 2.x).
>
> With this code:
>
> uint16_t a = 32000;
> uint32_t b = 64000;
> printf("a: %d (%d)\n", a, sizeof(a));
> printf("b: %d (%d)\n", b, sizeof(b));
>
> I get this output:
>
> a: 32000 (2)
> b: -1536 (0)
>
> Why do I get this strange output for "b"?
>
> David.
>
> PS: I also get these compiler warnings, but I'm not sure how serious they are:
>
> (for 2nd assignment) warning: decimal constant is so large that it is unsigned
> (for 1st printf) warning: int format, different type arg (arg 2)
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help