|
|
View previous topic :: View next topic |
Author |
Message |
Nazoa Guest
|
Signed 32 bit numbers |
Posted: Mon Jan 10, 2005 8:46 am |
|
|
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
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 10, 2005 12:38 pm |
|
|
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 |
Posted: Mon Jan 10, 2005 4:52 pm |
|
|
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 |
|
|
|
|
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
|