Tech Support

wrong result in multiplier

Q:

I am a newer in PIC C .    (HI-TECH Software\PICC\PRO\9.65) Lite.
 
In my project  (PIC 16F628A),
I find fail when multiplier.  (7 * 10.000; 8 * 10.000 and 9 * 10.000)
 
Then I write a litle code for test this conditionand the test fail too.
 
The code:
 
#include <htc.h>
//for test purpouse
long par0 = 0;   
long par1 = 0;
long par2 = 0;
long par3 = 0;
long par4 = 0;
long par5 = 0;
char par[]={1};
char tot =1;
void main(void){
tot =1;
   while (tot<10){   
   par0 = (tot&0x0F);  
   par1 = (tot&0x0F)*10;
   par2 = (tot&0x0F)*100;
   par3 = (tot&0x0F)*1000;
   par4 = (tot&0x0F)*10000;
   par5 = (tot&0x0F)*100000;
  
   tot++;        }
 
Results of multiplier:
 
DEC     1     10     100     1000     10000     100000
HEX     1     A        64      3E8        2710      186A0
      
DEC     2     20     200     2000     20000     200000
HEX     2     14     C8      7D0       4E20       30D40
      
DEC     3     30     300     3000     30000     300000
HEX     3     1E     12C     BB8       7530       493E0
      
DEC     4     40     400     4000     40000     400000
HEX     4     28     190     FA0      9C40         61A80
      
DEC     5     50     500     5000     50000     500000
HEX     5     32     1F4     1388      C350      7A120
      
DEC     6     60     600     6000     60000     600000
HEX     6     3C     258     1770     0EA60     927C0
      
DEC     7     70     700     000     70000     700000
HEX     7     46     2BC     1B58    11170     AAE60
Wrong  result >>>------>>>         1170
      
DEC     8     80     800     8000     80000     800000
HEX     8     50     320     1F40     13880     C3500
Wrong  result >>>------>>>          3880
      
DEC     9     90     900     9000     90000     900000
HEX     9     5A     384     2328     15F90     DBBA0
Wrong  result >>>------>>>          5F90
 
Loss digit "1" ?????
 
This is a bug, or my code  is not correct?
 

---------------------

A:

Try this:
 

   while (tot<10){    
        par0 = (tot&0x0F);   
        par1 = (tot&0x0F)*10L; 
        par2 = (tot&0x0F)*100L; 
        par3 = (tot&0x0F)*1000L; 
        par4 = (tot&0x0F)*10000L; 
        par5 = (tot&0x0F)*100000L; 
        tot++;         
    }



Then read the section 3.3 in the HiTech manual on data types.
 

Hits:  UpdateTime:2010-12-10 17:02:41  【Printing】  【Close

Tech Support

Hot Products

Topsee International Co.,LTD
Tel:0755-2699 3393
Email:info@topsee.com.cn
Web:www.topsee.com.cn