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

int32 int8 configurable?

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







int32 int8 configurable?
PostPosted: Wed Apr 14, 2004 9:41 pm     Reply with quote

I need to use int32 to do integer square roots using a function i saw in this newsgroup from a past post.

However, I don't want a 32 bit int to propagate throughout my whole program, and instead use an 8 bit int throughtout the majority of the program. Is #type configurable anywhere in the program? or is it a one time thing that i am not supposed to re use in different parts of the program.
Ttelmah
Guest







Re: int32 int8 configurable?
PostPosted: Thu Apr 15, 2004 2:42 am     Reply with quote

valemike wrote:
I need to use int32 to do integer square roots using a function i saw in this newsgroup from a past post.

However, I don't want a 32 bit int to propagate throughout my whole program, and instead use an 8 bit int throughtout the majority of the program. Is #type configurable anywhere in the program? or is it a one time thing that i am not supposed to re use in different parts of the program.

Types, do not 'propogate throughout the program'.
If you declare:
int8 fred, dick;
int16 tom, harry;

harry=tom*200L;

Then the arithmetic to calculate 'harry', will be done using 16bit maths.
However if on the next line you type:

fred=dick*20;

Then the arithmetiic to calculate 'fred' will use 8bit maths.
If you then type:

fred=tom/2;

The arithmetic will use 16bit maths (since tom is a 16bit variable), and the 'result' will then be converted to 8bit. However if you use:

fred=(int8)tom/2;

Tom will be converted to 8bit, and then divided by 2, using 8bit arithmetic.
If you then use:

tom=fred*2;

The cmpiler will calculate fred*2, using 8bit arithmetic, and convert the result to 16bit. Unfortunately this brings the risk of overflow. So if instead you use:

tom=(int16)fred*2;

or

tom=fred*2L;

This forces one of the two variables used in the calculation to be a 'long' before the arithmetic starts, and forces 16bit maths to be used.

The compiler switches the arithmetic used, according to the types of the variables involved in each part of the statement. There is a 'caveat' with this, in that it is easy to miss where the compiler may use 8bit arithmetic, in a section, where you think it i using a longer form, and data loss (overflows) may result. The 'plus', is that the compiler will tend to use shorter arithmetic routines, than if it always chose the 'longest' type in an entire statement.
Use the 'cast' (bracketing a number 'type' declaration in front of a variable), to force conversion to the type that you want to be used for the arithmetic.

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