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

Have to use multiple lines

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



Joined: 20 Jul 2009
Posts: 1

View user's profile Send private message

Have to use multiple lines
PostPosted: Wed Jan 29, 2014 11:27 am     Reply with quote

If I type this following into 1 line, I cannot get any results for vmem1 but if I split it up into multiple lines it works. What am I doing wrong?

V4.132

Code:

   sprintf(vm,"%04lu",cntalive);
   vmem1 = (int16)(vm[0]-'0') <<12 + (int16)(vm[1]-'0') <<8 + (vm[2]-'0') <<4 + (vm[3]-'0');


Code:

   sprintf(vm,"%04lu",cntalive);
   vmem1 = (int16)(vm[0]-'0') <<12;
   vmem1 += (int16)(vm[1]-'0') <<8;
   vmem1 += (vm[2]-'0') <<4;
   vmem1 += (vm[3]-'0');
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 29, 2014 11:35 am     Reply with quote

Look at a table of Operator Precedence in C. Which operator is done
first, left-shift or addition ? How can you force operator precedence ?
Answer those questions and you will probably solve your problem.
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 29, 2014 11:43 am     Reply with quote

just a comment: anything that complicated is best done in several lines. Visually you can 'see' how the code should work and is easier to spot problems like PCM P says , precedence, braces, etc.
As well, you should test code like that with known inputs -> outputs to verify that it is correct over the desired range of variables.Don't just test for a couple of cases(like 00 of 0xff) ! You might think it's OK but then 'funny' things happen...
Once you know for sure it works AND you need either more speed or less code space THEN reduce the code, just be sure to test !

hth
jay
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