View previous topic :: View next topic |
Author |
Message |
Marco27293
Joined: 09 May 2020 Posts: 126
|
PIC18F47J53 CCS C Compiler 5.090 bit shift |
Posted: Tue Jul 21, 2020 9:24 am |
|
|
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: 9228 Location: Greensville,Ontario
|
|
Posted: Tue Jul 21, 2020 10:40 am |
|
|
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: 19516
|
|
Posted: Tue Jul 21, 2020 11:49 am |
|
|
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: 1349
|
|
Posted: Tue Jul 21, 2020 1:50 pm |
|
|
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. |
|
|
|