View previous topic :: View next topic |
Author |
Message |
Tagge
Joined: 23 Aug 2005 Posts: 93
|
Type casting? |
Posted: Thu Oct 04, 2007 9:27 am |
|
|
Hi, found a lot on this forum about this, but im not sure how it works..
I want to:
Code: |
signed int32 value;
float multi;
value=99;
multi=0.99;
value*=multi;
|
Now I would like that value to be 98, and still a signed int32.
But what is it in CCS?
What happens if:
value*=(signed int32)multi;
Or what happens if value=-99;
And what happens if value = 0;
/Tagge |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 04, 2007 4:57 pm |
|
|
Make a test program and do experiments. You can run the program
on a demo board and display the output in a terminal window on your PC.
Or, you can run it in the MPLAB simulator and display the output in the
"Output Window" of MPLAB, by using the "UART1" feature of the MPSIM
simulator. Here are instructions on how to do that:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
By using the MPLAB simulator, you can quickly test a simple program.
Here's the test program, and here's the output:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//====================================
void main()
{
signed int32 value;
float multi;
value = 99;
multi = 0.99;
value *= multi;
printf("%ld \n\r", value);
while(1);
} |
|
|
|
Tagge
Joined: 23 Aug 2005 Posts: 93
|
|
Posted: Mon Oct 08, 2007 2:01 am |
|
|
Yes, it do work on positive numbers, but for example
Code: |
float multi=0.0;
signed int32 first=0;
signed int32 second=0;
signed int32 third=0;
while(1)
{
multi=0.99;
first=99;
first*=multi; //=98 = ok
second=-100;
second*=multi;//=0xFFFFFF9D, this is not ones complement?
third*=multi; //0*0.99=0 = ok
}
|
In this case the first and third is ok, but the second gets 0xFFFFFF9D.
So, negative values are used as?
/Tage |
|
|
Ttelmah Guest
|
|
Posted: Mon Oct 08, 2007 2:35 am |
|
|
Signed values, are stored in _two's complement_, not _one's complement_....
-99 is 0xFFFFFF9D in two's complement arithmetic.
Best Wishes |
|
|
Tagge
Joined: 23 Aug 2005 Posts: 93
|
|
Posted: Mon Oct 08, 2007 3:17 am |
|
|
Hi again and thanks, this is just the best forum on the net..
Here is a twos complement table for other to use
8-bit two's-complement integers
sign bit
0 1 1 1 1 1 1 1 = 127
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 0 0 = 0
1 1 1 1 1 1 1 1 = −1
1 1 1 1 1 1 1 0 = −2
1 0 0 0 0 0 0 1 = −127
1 0 0 0 0 0 0 0 = −128
Thanks/Tagge |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Oct 08, 2007 4:36 am |
|
|
8-bit two's-complement integers
sign bit
0 1 1 1 1 1 1 1 = 127
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 0 0 = 0
1 1 1 1 1 1 1 1 = −1
1 1 1 1 1 1 1 0 = −2
1 0 0 0 0 0 0 1 = −127
1 0 0 0 0 0 0 0 = −128
Twos complement notation has the advantage that any number and its twos complement will add to 00000000 in a physical register. Now if -2 was notated as 10000010 ( msb is the sign) the addition of two and minus two wouldn't end up easily as 0000000 as is the case with two's complement notation. Two's complement is efficient since its notation of the number allows the processor to perform mathematical operations without much conversion from notational value to physical register value. |
|
|
|