View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Product of hexadecimal numbers |
Posted: Fri Aug 07, 2009 3:16 pm |
|
|
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 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 07, 2009 3:22 pm |
|
|
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
|
|
Posted: Fri Aug 07, 2009 4:07 pm |
|
|
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
|
|
Posted: Fri Aug 07, 2009 4:20 pm |
|
|
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:
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
|
|
Posted: Fri Aug 07, 2009 4:54 pm |
|
|
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
|
|
Posted: Fri Aug 07, 2009 5:16 pm |
|
|
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
|
|
Posted: Mon Aug 10, 2009 2:26 am |
|
|
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 |
|
|
|