|
|
View previous topic :: View next topic |
Author |
Message |
Hans Wedemeyer Guest
|
Cast Problem (updated) |
Posted: Sun Aug 03, 2003 11:29 am |
|
|
<font face="Courier New" size=-1>#define PAGE_SIZE 16384
#define CHIP_SIZE 32768
BYTE Page;
BYTE Chip;
Page = 2;
Chip = (BYTE)((unsigned long)Page * (unsigned long)PAGE_SIZE) / (unsigned long)CHIP_SIZE;
or Chip = ( 2 * 16384 ) / 32768
Chip should == 1 in PCWH always equals 0
If I use variables for PAGE_SIZE and CHIP_SIZE it works, but that means I have to load the variables with constants anyway, and that eats space and time... It should be valid to cast the constants to unsigned long.
Hans W</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516631 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Cast Problem (updated) |
Posted: Sun Aug 03, 2003 1:30 pm |
|
|
:=#define PAGE_SIZE 16384
:=#define CHIP_SIZE 32768
:=
:=BYTE Page;
:=BYTE Chip;
:=
:=Page = 2;
:=Chip = (BYTE)((unsigned long)Page * (unsigned long)PAGE_SIZE) / (unsigned long)CHIP_SIZE;
:=
:=or Chip = ( 2 * 16384 ) / 32768
:=
:=Chip should == 1 in PCWH always equals 0
:=
:=If I use variables for PAGE_SIZE and CHIP_SIZE it works, but that means I have to load the variables with constants anyway, and that eats space and time... It should be valid to cast the constants to unsigned long.
--------------------------------------------------------------
It works if parens are put around the entire equation.
The terminal window displays this, for the following test pgm:
chip = 0
chip = 1
<PRE>
#include "c:\Program Files\Picc\Devices\16F877.H"
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 8000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
<BR>
<BR>
#define PAGE_SIZE 16384
#define CHIP_SIZE 32768
<BR>
void main()
{
BYTE Page;
BYTE Chip;
<BR>
Page = 2;
Chip = (BYTE)((unsigned long)Page * (unsigned long)PAGE_SIZE) / (unsigned long)CHIP_SIZE;
printf("chip = \%u\n\r", chip);
<BR>
<BR>
Page = 2;
Chip = (BYTE)( ((unsigned long)Page * (unsigned long)PAGE_SIZE) / (unsigned long)CHIP_SIZE);
printf("chip = \%u\n\r", chip);
<BR>
<BR>
while(1);
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516632 |
|
|
R.J.Hamlett Guest
|
Re: Cast Problem (updated) |
Posted: Sun Aug 03, 2003 2:37 pm |
|
|
:=<font face="Courier New" size=-1>#define PAGE_SIZE 16384
:=#define CHIP_SIZE 32768
:=
:=BYTE Page;
:=BYTE Chip;
:=
:=Page = 2;
:=Chip = (BYTE)((unsigned long)Page * (unsigned long)PAGE_SIZE) / (unsigned long)CHIP_SIZE;
:=
:=or Chip = ( 2 * 16384 ) / 32768
:=
:=Chip should == 1 in PCWH always equals 0
:=
:=If I use variables for PAGE_SIZE and CHIP_SIZE it works, but that means I have to load the variables with constants anyway, and that eats space and time... It should be valid to cast the constants to unsigned long.
:=
:=Hans W</font>
You are actually generating (2 * 16384), and casting this to a byte, then dividing this by 32768...
The cast affects the bracketted section beyond it.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516633 |
|
|
Hans Wedemeyer Guest
|
Thanks for the help... |
Posted: Sun Aug 03, 2003 5:32 pm |
|
|
Thanks for hte heads up... too many braces and brackets I got lost in them ...tree and forrest problem...
Hans W
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516635 |
|
|
R.J.Hamlett Guest
|
Re: Thanks for the help... |
Posted: Mon Aug 04, 2003 2:21 am |
|
|
:=Thanks for hte heads up... too many braces and brackets I got lost in them ...tree and forrest problem...
:=Hans W
One other comment. There is actually a much better/easier way of preventing this problem!... The 'heart' of this, is that the default integer size in CCS, is the int8. Now it is therefore very easy to forget, when you are used to the 'commonest' C configuration, where the default integer is either an int16, or an int32, that multiplication of constants like '16384', cannot be handled in the default size. You have shown the versions with 'casts', to ensure that the maths does propogate to use the larger sizes, but there is a method, that to my mind, makes the code tidier, and avoids this happening...
What is this magic solution?. The answer is to ensure that _whenever_ you define a constant that is larger than an int8, you add the 'L' to show that this is to be handled as a long. So in your code example, the following will solve the problem, without casting:
#define PAGE_SIZE 16384L
#define CHIP_SIZE 32768L
BYTE Page;
BYTE Chip;
Page = 2;
Chip = Page * PAGE_SIZE /CHIP_SIZE;
The 'L', now forces all the arithmetic to use 'longs'. The final cast 'down' to a byte, is one that the compiler allways seems to handle fine.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516639 |
|
|
Hans Wedemeyer Guest
|
Re: Thanks for the help... |
Posted: Mon Aug 04, 2003 7:10 am |
|
|
good idea...
thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516650 |
|
|
|
|
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
|