[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] integer overflow
From: |
eric |
Subject: |
Re: [avr-gcc-list] integer overflow |
Date: |
Tue, 27 May 2003 23:21:16 GMT |
> Ömer,
>
> > i have an overflow warning from the compiler
> > here is the code that makes the problem
> > unsigned long val;
> >
> > val = 1000*60*60*24;
> >
> > i think the problem stems from the sizeof long , as far
as i know long
> > is 4 bytes for atmel processors and integer is 2
bytes.the maximum
> > value for 4 bytes is
> >
> > FFFFFFFF which is 4,294,967,295 that is bigger than
86,400,000.
> >
> > if long is 2 bytes than is there a built in structure
for carrying 4
> > bytes ?
>
> Try...
>
> val = 1000L*60*60*24;
>
> "Ints" are 2 bytes with "avr-gcc". Integer constant
expressions
therefor> e
> use only 2 bytes, unless you somehow promote the
expression evaluation to
> use 4-byte longs. Replacing 1000 by 1000L will cause
this to happen.
>
> Best regards,
>
> Bruce
>
> P.S.- There are many other ways to do this, which I'm
sure Joerg will
soo> n
> share with you...
Yeah? You're only half right.
If you read Ömer's post, val is an unsigned long.
So you need to do:
val = 1000UL*60*60*24;
to make it an unsigned long constant to match the type of
the variable.
Eric