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

Type casting?

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



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

Type casting?
PostPosted: Thu Oct 04, 2007 9:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 04, 2007 4:57 pm     Reply with quote

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:
Quote:
98

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Oct 08, 2007 2:01 am     Reply with quote

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







PostPosted: Mon Oct 08, 2007 2:35 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Oct 08, 2007 3:17 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Mon Oct 08, 2007 4:36 am     Reply with quote

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.
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