View previous topic :: View next topic |
Author |
Message |
colin_turner99
Joined: 24 Nov 2005 Posts: 7
|
int8 & int16 |
Posted: Fri Apr 07, 2006 9:04 am |
|
|
Hi
Please can you help me i have this sum in my code
Code: |
int16 time2;
int8 minutes = 58;
int8 seconds = 05;
time2 = (minutes*60)+seconds;
|
when i do this in the pic i get the result 38917
but when i do it in Excel i get 3485
i am not sure what is going on
Thanks in advance
Colin |
|
![](templates/subSilver/images/spacer.gif) |
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Fri Apr 07, 2006 9:16 am |
|
|
I would make minutes an int16 as well, because minutes*60 is more than 8 bits. |
|
![](templates/subSilver/images/spacer.gif) |
colin_turner99
Joined: 24 Nov 2005 Posts: 7
|
|
Posted: Fri Apr 07, 2006 9:32 am |
|
|
Hi
Thanks for your suggestion
I have tried that and didnt make any differance
Colin |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Fri Apr 07, 2006 9:48 am |
|
|
Quote: |
int16 time2;
int8 minutes = 58;
int8 seconds = 05;
time2 = (minutes*60)+seconds;
|
Not sure how the compiler interprets the int8 seconds = 05, but it should work. Try this:
Code: |
time2 = ((int16)minutes * 60) + 5;
|
If this works then replace the 5 with seconds and see if you get the same result. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Apr 07, 2006 10:03 am |
|
|
Are you sure the sum is as you show?. The result you are getting, is 0x9805. Now this would correspond exactly to what you should get for the first part of the sum (50*60, solved in 'int8' arithmetic, gives 0x98), shifted left eight times, and then with 5 added!..
The 'correct' way to type this sum, is:
time2=((int16)minutes*60)+(int16)seconds;
Or:
time2=(minutes*60L)+(int16)seconds;
Both of which should give 3485!.
I'd suspect the problem is that the 'overflow' flag is set by the first part of the sum, and somehow this results in the returned value bing treated as if it is the MSB for the return. A definately 'unexpected' result....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|