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

Cant multiple int16*int16

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







Cant multiple int16*int16
PostPosted: Mon Apr 09, 2007 2:46 am     Reply with quote

Hi
i have to multiple two unsigned int16 numbers.

unsigned int32 result;
unsigned int16 number1;
unsigned int16 number1;

code is like this;

result=number1*number2;

then i am showing the result on LCD
if the number1 is greater then 300 (or equals) then the result is lower as 256*number2

for example
number1=10;
nuber2=105;
result=1050 this works fine

but
when number1=300;
number2=1;
result=44;

what am i missing
Ttelmah
Guest







PostPosted: Mon Apr 09, 2007 7:08 am     Reply with quote

Start by correcting your typing errors!. You are declaring 'number1' twice in what you post, and not showing the declaration of number2 at all. Now what you are seeing, implies that probably you have 'number1' really declared as an int (8 bit int), not an int16.
As a separate comment though, there is no point in declaring the result as int32 in what you show. By default, CCS, only return the low 16 bits of a 16*16 multiplication. If you want a 32bit result, then you need:
Code:


unsigned int32 result;
unsigned int16 number1;
unsigned int16 number2;

number1=100;
number2=1050;

result=(int32)number1*number2;

This will correctly handle generating the 32bit result from the 16*16 multiplication.

Best Wishes
KAZMA
Guest







PostPosted: Mon Apr 09, 2007 7:34 am     Reply with quote

there are no type errors in my orginal code.
and i tried

result=(int32)number1*number2;

i still get the same result.

thanks
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Apr 09, 2007 8:25 am     Reply with quote

I would suggest that you listen to the suggestions of the pros that take a significant amount of time to help a lot of us amateurs. They see things immediately that we can't see after staring for hours. Telling one of them they are wrong will quickly get you a place on the 'ungrateful, do not assist' list.

Quote:
there are no type errors in my orginal code.


There aren't?

unsigned int32 result;
unsigned int16 number1;
unsigned int16 number1;

Ttelmah was kind enough to point out an obvious error. Make the effort to listen.

Read the guidelines for posting and follow them.

If you are going to post code....

Use the code posting feature of the BB.

Disable HTML.

Cut and paste, don't re-type...
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Apr 09, 2007 8:29 am     Reply with quote

Would it need to have both variables cased and be written like this:

Code:
result = (int32)number1 * (int32)number2;


Just a quick thought.

Ronald
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Apr 09, 2007 8:36 am     Reply with quote

I was wondering a similar thought.... out of ignorance I typically cast like this:

Code:
result = (int32)(number1 * number2);
KAZMA
Guest







PostPosted: Mon Apr 09, 2007 8:52 am     Reply with quote

Hi everyone
i solved the problem thanks everybody,

@jecottrell if you read it again i said
there are no type errors in my ORGÄ°NAL code this here is just a sample,

and i didnt say to anyone they are wrong, i said just i dont any type errors

anyway

in my code i was generating the numbers from something other

unsigned int8 xx;

number1=xx*1000;

the problem was that the variable xx was int8 i changed the definition of variable xx to int16 then it worked

i was using CC5x there werent problems like this

thanks again
Ttelmah
Guest







PostPosted: Mon Apr 09, 2007 9:02 am     Reply with quote

jecottrell wrote:
I was wondering a similar thought.... out of ignorance I typically cast like this:

Code:
result = (int32)(number1 * number2);


Key is that in a mathematical operation, the work is done in the type of the 'highest' component. Hence if you multiply an int16 by an int16, the arithmetic is done using int16. However if you multiply an int32 by an int16, int32 arithmetic is used. So you only 'need' to cast one part, but casting both does no harm. Smile

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