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

Arithmetric Addition for 32-bit integer

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



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

Arithmetric Addition for 32-bit integer
PostPosted: Thu Apr 08, 2004 7:50 pm     Reply with quote

I am trying to perform arithmetric addition for integers of more than 16-bits. The following is the code that I use but it does not allow SUM of variable 'a' and 'b' that exceed 16-bits ie. less than 65536 only.

What shall I do to allow it to calculate the SUM of more than 16-bit say 32 bits?

Cheers...

#include <16f877a.h>
//#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <utility.c>
#include <stdlib.h>
#include <input.c>


#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4


main()
{

int32 a,b,result;
char opr;
int8 digits[7];
signed int8 counter;
int32 val;
int16 low;
int16 high;



setup_timer_0(RTCC_INTERNAL);
while(TRUE)
{
printf("\r\nEnter the first number: ");
a=get_long();

do
{
printf("\r\nEnter the operator (+-*/): ");
opr = getc();
}
while(!isamoung(opr,"+-*/"));

printf("\r\nEnter the second number: ");
b = get_long();

switch(opr)
{
case '+':
result = a + b;
break;

case '-':
result = a - b;
break;

case '*':
result = a * b;
break;

case '/':
result = a / b;
break;
}

/////////////////////////////////////////////////////////////////
val = a;

for (counter=7;counter>-1;counter--)
{
digits[counter]=val%10;
val=val/10;
}

low=result % 1000L;
high=result / 1000L;
printf("\r\nThe result is %lu%lu ",high,low);
// printf("\r\nVal= %d %d %d %d %d %d",digits[0],digits[1],digits[2],digits[3],digits[4],digits[5],digits[6]);

}

}
Guest








PostPosted: Fri Apr 09, 2004 1:25 am     Reply with quote

get_long() function returns 16-bit number (long is 16 bit wide in CCS C).
You have to modify the function to use atoi32() instead of atol() and return a 32-bit value:

signed int32 get_int32() {
char s[11];
signed int32 i;

get_string(s, 11); // able to get max. cca. 4 000 000 000

i=atoi32(s);
return(i);
}
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