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 support@ccsinfo.com

PIC18F47J53 CCS C Compiler 5.090 bit shift

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



Joined: 09 May 2020
Posts: 110

View user's profile Send private message

PIC18F47J53 CCS C Compiler 5.090 bit shift
PostPosted: Tue Jul 21, 2020 9:24 am     Reply with quote

Hi,

Is there any diference between:

Code:
for(k=0;k<(Hex_File_Info[Trunks_number]<<1);k+=2)                                       
   {
      err+=M24M02_random_read_2byte_Loader(1,0x208+(k<<1),&trunc_address[k]);
      err+=M24M02_random_read_2byte_Loader(1,0x20A+(k<<1),&trunc_address[k+1]);
   }

//and

for(k=0;k<(Hex_File_Info[Trunks_number]*2);k+=2)                                       
   {
      err+=M24M02_random_read_2byte_Loader(1,0x208+(k*2),&trunc_address[k]);
      err+=M24M02_random_read_2byte_Loader(1,0x20A+(k*2),&trunc_address[k+1]);
   }


k RAM variable value behaviour is the same or not?
temtronic



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

View user's profile Send private message

PostPosted: Tue Jul 21, 2020 10:40 am     Reply with quote

The easiest way to see is cut a small program with both versions, compile it, then dump the listing and look at the code that was generated.

The compiler is smart and probably knows <<1 is same as *2 BUT you really should look and see

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Jul 21, 2020 11:49 am     Reply with quote

It depends on the size and type of 'k'.
Bit rotation is only guaranteed to be a *2, on an positive value, with
less than one minus the number of bits held by the number of bits
storable by the value.

So if you have a signed int16, if the rotation goes out of the fifteenth bit,
the equivalence is not guaranteed. Similarly rotation of a -ve number
does not have this guaranteed equivalence.
jeremiah



Joined: 20 Jul 2010
Posts: 1314

View user's profile Send private message

PostPosted: Tue Jul 21, 2020 1:50 pm     Reply with quote

Also keep in mind that per the C standard, left shifts of signed integers are potentially undefined (Section 6.5.7/4 of the standard), so make sure any data that you left shift is not signed.
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