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 CCS Technical Support

Product of hexadecimal numbers

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Product of hexadecimal numbers
PostPosted: Fri Aug 07, 2009 3:16 pm     Reply with quote

Hi what is the problem in this code:

Code:

int16 New_number ;

New_number = ((0x017 * 0x0E10) );


I can not get the correct result, the result should be: 0x14370 but I get 17264, which is my mistake Embarassed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 07, 2009 3:22 pm     Reply with quote

Quote:
int16 New_number;

The result should be: 0x14370

What is the largest hex value that an 'int16' can hold ?

This is in the CCS manual.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Fri Aug 07, 2009 4:07 pm     Reply with quote

I am sorry, I had a mistake but, despite the correction result is the same I can not get the correct result

Code:
int32 New_number ;

New_number = ((0x017 * 0x0E10) );
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 07, 2009 4:20 pm     Reply with quote

Make a test program. Run it. I'm using MPLAB simulator with output
displayed by "UART1" in the MPLAB Output Window. I get the following
output:
Quote:
00014370

Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
int32 New_number;

New_number = 0x017 * 0x0E10;

printf("%lx \n\r ", New_number);

while(1);
}

I tested this with vs. 4.094.
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Fri Aug 07, 2009 4:54 pm     Reply with quote

Hi PCM programmer, thank you by your help, but now I'm really confused, when I use my debugger the value of New_number is 17264 in decimal but when I use the UART1 as you suggests me I get the correct value New_number= 00014370 but in hexadecimal, why a obtain 17264 and not 00014370???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 07, 2009 5:16 pm     Reply with quote

The number base of the output (hex or decimal, etc.) is set by the
format string inside the printf function. This is explained in the printf
section of the CCS compiler manual.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Aug 10, 2009 2:26 am     Reply with quote

pilar wrote:
when I use my debugger the value of New_number is 17264 in decimal but when I use the UART1 as you suggests me I get the correct value New_number= 00014370 but in hexadecimal, why a obtain 17264 and not 00014370???


I may be able to answer this, although I do not know for sure but,
A compiler would normally take the 2 const values and do the math at compile time, this would give you a 32 bit value and look like this after the first pass.

New_number = 0x14370; // Comiler did the math before linking.

Where as when using the debugger and it compiles a debugger version of the code it actually does the math within the code so

New_number = 0x017 * 0x0E10;

When executed would produce
17264 or 0x4370 Notice the 1 is missing from the front of the hex value! This is because 1 of the values is 8 bit and the other is explicitly 16 bit so CCS would have compiled it to do 16 bit math.
(int16)0x017 * (int16)0x0E10 = (int16)0x4370

Try casting one or both values to 32bit.
(int32)0x017 * 0x0E10 = 0x14370

You can confirm this by looking at the .lst file
Just a hunch Smile
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