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 long array behaving as a long array [SOLVED]

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



Joined: 03 Jan 2012
Posts: 4
Location: Pamplona

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

signed long array behaving as a long array [SOLVED]
PostPosted: Tue Jan 17, 2012 11:00 am     Reply with quote

Hi everyone,

I'm having an issue with a "signed long array" declared as a global variable.

Code:

signed long U[4]={0,0,0,0};
long lambda[25][2];   


Code:


   signed long sal,consecuente[5]={-15,-10,0,10,15};
   float num,den,aux;
   int i,cons;

   for(i=0;i<25;i++)   {
   
      cons=lambda[i][1];
      aux=lambda[i][0];
      aux=aux*consecuente[cons];
      num=aux+num;
       den=lambda[i][0]+den;

   }
   
   sal=num/den;
   U[j]=sal;


When the result of the function above is negative (e.g.: -15) sal=15, but I get U[j]=65521 (65536-15) instead of -15.

Has someone had the same issue? I'm programming a PIC18F27J53 using MPLAB 8.8 with CCS 4.120 plugin.
Thanks in advance
_________________
CME IngenierĂ­a
www.cmeingenieria.es


Last edited by cmesparza on Tue Jan 17, 2012 1:40 pm; edited 1 time in total
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Tue Jan 17, 2012 11:05 am     Reply with quote

I may be misunderstanding your issue, but 65521 is the signed 2's compliment 16 bit representation of -15, which is what I would expect.

65535 = -1
65534 = -2
65533 = -3
...
65522 = -14
65521 = -15
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Jan 17, 2012 11:33 am     Reply with quote

How are you examining the result? Is the problem in your print function, or maybe in MPLAB's display routines?
_________________
The search for better is endless. Instead simply find very good and get the job done.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue Jan 17, 2012 12:45 pm     Reply with quote

All numbers are represented by notation. 2's complement is a convenient notation for the operation on registers by the PIC. We are used to decimal notation so we must be aware of the decimal translation we request for a PIC register. A PIC register can notate an ascii char a signed integer an unsigned integer or maybe just a piece of a larger number that requires more than one register and many other notations. The register is transistors in binary states so the appropriate translation to decimal notation must be specified. Signed notation incorrectly specified as unsigned will result in a wrong decimal translation.
cmesparza



Joined: 03 Jan 2012
Posts: 4
Location: Pamplona

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

PostPosted: Tue Jan 17, 2012 1:40 pm     Reply with quote

You were all right, I was looking at the value of the variable, not to the decimal value.
The program works perfectly, I wasn't thinking in the 2's complement... Embarassed

Thanks!
_________________
CME IngenierĂ­a
www.cmeingenieria.es
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