CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

int8 & int16

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
colin_turner99



Joined: 24 Nov 2005
Posts: 7

View user's profile Send private message

int8 & int16
PostPosted: Fri Apr 07, 2006 9:04 am     Reply with quote

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
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Fri Apr 07, 2006 9:16 am     Reply with quote

I would make minutes an int16 as well, because minutes*60 is more than 8 bits.
colin_turner99



Joined: 24 Nov 2005
Posts: 7

View user's profile Send private message

PostPosted: Fri Apr 07, 2006 9:32 am     Reply with quote

Hi

Thanks for your suggestion

I have tried that and didnt make any differance

Colin
Guest








PostPosted: Fri Apr 07, 2006 9:48 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Fri Apr 07, 2006 10:03 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group