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

Adding 8-bit integers and storing the result in an 8-bit int

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



Joined: 05 Jul 2018
Posts: 18

View user's profile Send private message

Adding 8-bit integers and storing the result in an 8-bit int
PostPosted: Fri Jan 11, 2019 11:58 am     Reply with quote

If I add three 8-bit integers, how will the result be saved in an 8-bit integer.

Eg:
Code:

#use RS232(BAUD=9600, XMIT=PIN_B6, RCV=PIN_B7, FORCE_SW)

int8 high_byte = 200;
int8 low_byte = 150;
int8 element = 100;
int8 sum = 0;

void main()
{
sum = high_byte + low_byte + element ;
putc(sum);
}


I know this is not the ideal situation and when two or more 8-bit integers are added, there is a possibility of the result to exceed 255 and thereby requiring a 16-bit integer to store the result.

But what if the result or sum is placed in an 8-bit integer? Will the carry bits be avoided? or will it store random data?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Jan 11, 2019 12:02 pm     Reply with quote

It'll store the low 8bits of the result.
Bits that overflow will just be lost.
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