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

Multiplication operator

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



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

Multiplication operator
PostPosted: Sun Mar 23, 2014 2:20 am     Reply with quote

hi
Code:
#include <16f84a.h>   
#use delay(Xtal=4Mhz)
void main  ()
{
int8 ton_m=100;
int16 tonm;
tonm=(ton_m*10);
while(true)
{
output_high(pin_b7);
delay_us(tonm);
output_low(pin_b7);
delay_ms(17);
}
}


when int8 ton_m, Multiplication operation is not performed and tonm=ton_m=100!!!
but when int16 ton_m, Multiplication operation is performed and tonm=1000;

why???
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Re: Multiplication operator
PostPosted: Sun Mar 23, 2014 2:42 am     Reply with quote

hamid9543 wrote:
hi
Code:
#include <16f84a.h>   
#use delay(Xtal=4Mhz)
void main  ()
{
int8 ton_m=100;
int16 tonm;
tonm=(ton_m*10);
while(true)
{
output_high(pin_b7);
delay_us(tonm);
output_low(pin_b7);
delay_ms(17);
}
}


when int8 ton_m , Multiplication operation is not performed and tonm=ton_m=100!!!
but when int16 ton_m ,Multiplication operation is performed and tonm=1000;

why???

How do you KNOW what is happenng?

Mike
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

mull
PostPosted: Sun Mar 23, 2014 3:20 am     Reply with quote

Connect pin_b7 to oscilloscope and measure time.
Ttelmah



Joined: 11 Mar 2010
Posts: 19329

View user's profile Send private message

PostPosted: Sun Mar 23, 2014 4:04 am     Reply with quote

The key is to understand 'types' in C.

ton_m, is an int8. Max 255. When you perform arithmetic in C, the type used for the arithmetic, is the _highest_ of the two values being used. You have an int8 (ton_m), and another int8 (10). So int8 arithmetic is used. 100*10, then overflows the arithmetic. You should get 232 as the result (0x3E8, but only 'E8' retained).
Now, you can handle this multiple ways:
Code:

tonm=((int16)ton_m*10);
//This converts ton_m to an int16 _before_ the arithmetic, so int16 is used

tonm=(ton_m*10L);
//This says to treat the '10' as an int16


Now this is how 'C' originally defined arithmetic 'should' be done, but on most 'larger' modern C's, it is not how it happens.
Reasons are that first the 'default' internal integer is an int16 on most systems. So the '10' gets assumed to be an int16, unless you specifically 'say' this is an int8. Then second, on most systems there is hardware to perform a 16bit multiplication, and this is used automatically to do the int8 multiplication, so the overflow gets implicitly handled.
However the same lesson applies with all C's. As a programmer, you need to ensure that the maths size being used, is large enough to handle the sum.

The advantage of this approach, is that it keeps code size small, since when a smaller sum is all that if needed, extra code is not added.

Multiplication _is_ being performed, but only int8 multiplication.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Re: mull
PostPosted: Tue Mar 25, 2014 2:45 am     Reply with quote

hamid9543 wrote:
Connect pin_b7 to oscilloscope and measure time.

Which is what I guessed you might have done, hence the question.

However, you have not measured carefully enough.
When I tried your int8 version, the result of the multiply is the expected 232.
The multiplication does take place, just as Mr T explains.

Mike
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

Re: mull
PostPosted: Thu Mar 27, 2014 4:37 am     Reply with quote

Mike Walne wrote:
just as Mr T explains.


10*100 don't fit in no int8 Foo

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