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

Signed 32 bit numbers

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







Signed 32 bit numbers
PostPosted: Mon Jan 10, 2005 8:46 am     Reply with quote

Hello,

I want to get the PIC to substract two 32 bit numbers. Now, I know that the result (including a sign bit) will always fit in 3 bytes, so I want to send these three bytes out to the RS232 port. I want the MSB of the 3 byte number to be the sign digit and I do not want 2s complement. I am proposing to use something as follows.

amp2=amp2-amp3; //Get difference
if bit_test(amp2,31) bit_set(amp2,23); //Negative sign for 3 byte

// Send out data
putc(make8(amp2,2)); // CH2-3, msb first
putc(make8(amp2,1));
putc(make8(amp2,0));

I need some guidance as I am not sure how 32 bit numbers are handled. Any comments or suggestions?

Thanks
Nils
Question
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 10, 2005 12:38 pm     Reply with quote

32-bit signed integers are stored in two's complement format.

If you know for certain that the result of the subtraction will
always be 24-bits or less, then you could just throw away the
upper byte. Example:

Max positive value for 24-bits = 0x7FFFFF (8388607 decimal)

Max negative value for 24-bits = 0x800000 (-8388608 decimal)

In 32-bit format, those values are 0x007FFFFF and 0xFF800000,
so you can see that the upper byte can be discarded, and you
will still have the correct number in 24-bit format.


Why don't you want 2's complement format ? It allows the full
24-bits to be used. You get the widest range of numbers with it.
nazoa
Guest







32 bit signed integers
PostPosted: Mon Jan 10, 2005 4:52 pm     Reply with quote

Thanks for the suggestion, I understand. Yeah, the thing is that the data is being sent to a PC that is not expecting 2s complement. I guess that I can get around that with some extra code.

Thanks again.
Nils
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