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

Assemble long int from shifted bytes

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



Joined: 03 Jun 2020
Posts: 37
Location: UK

View user's profile Send private message

Assemble long int from shifted bytes
PostPosted: Tue Jan 10, 2023 6:10 am     Reply with quote

I receive an int32 as four int8 and I need to reconstruct the original int32

e.g. Byte3 = 32, byte2 4, byte1 = 8 and byte0 =99

longInt = (Byte3<<24) gives the correct value of 536870912 but

longInt = (Byte3<<24) + (Byte2<<16) (or any other combination of shifted bytes) gives zero.

How does one add shifted bytes to form a long int?

code snip:
Code:
   PulseInt = 537135203; // test value, all bytes non-zero
   // create the received bytes
   Byte3 = PulseInt >> 24;
   Byte2 = (PulseInt >> 16) & 0xFF;
   Byte1 = (PulseInt >> 8) & 0xFF;
   Byte0 = PulseInt & 0xFF;
   // the individual byte values are correct
   Interval = Byte3<<24;         // OK
   Interval = (Byte3<<24) + (Byte2<<16); // not OK
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 6:52 am     Reply with quote

Why not use the make() function that CCS supplies ?

quik cut and paste from 2019 manual..

make32( )
Syntax:
i32 = MAKE32(var1, var2, var3, var4)
Parameters:
var1-4 are a 8 or 16 bit integers
var2-4 are optional
Returns:
32 bit integer
Function:
Makes a 32 bit number out of any combination of 8 and 16 bit numbers. Note that the
number of parameters may be 1 to 4. The msb is first. If the total bits provided is less
than 32 then zeros are added at the msb

Availability:
All Devices
Requires:
-----
Examples:
int32 x;
int y;
long z;
x = make32(1,2,3,4); // x is 0x01020304
y=0x12;
z=0x4321;
x = make32(y,z); // x is 0x00124321
x = make32(y,y,z); // x is 0x12124321
Example Files:
ex_freqc.c
See Also:


make() is also very fast to execute.
colin382



Joined: 03 Jun 2020
Posts: 37
Location: UK

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 7:28 am     Reply with quote

Thanks temtronic, that worked perfectly.

"Why not use the make() function that CCS supplies ?"
because I didnt know it existed, nor did I expect it to exist!
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 7:31 am     Reply with quote

It's worth also saying that the same applies to your extraction operations:
Code:

   PulseInt = 537135203; // test value, all bytes non-zero
   // create the received bytes
   Byte3 = make8(PulseInt,3; //Take the top byte from the value
   Byte2 = make8(PulseInt,2); //take the third byte
   Byte1 = make8(PulseInt,1); //second byte
   Byte0 = make8(PulseInt,0); //bottom byte


You will find that this code is smaller and quicker than your version.
The make8 operation just extracts the single 8bit byte from a variable.
Just what you want. The make32 operation does effectively the reverse
(they are both more capable than this so make32, can for example
assemble two 16bit values).

It is also worth raising the other alternative way of doing this with a
union:
Code:

   union {
       unsigned int32 whole;
       unsigned int8 Bytes[4];
   } PulseInt, Interval;
   PulseInt.whole = 537135203;

   //Then PulseInt.Bytes[0] to [3] are directly the bytes

   Byte3 = PulseInt.Bytes[3];
   Byte2 = PulseInt.Bytes[2];
   Byte1 = PulseInt.Bytes[1];
   Byte0 = PulseInt.Bytes[0];
   //as a demo of this.

   //You can then reassemble the other way round.

   Interval.Bytes[3]=Byte3;
   Interval.Bytes[2]=Byte2;

   //etc...


The great thing about both the union and the make accesses is the
compiler does both by simple direct byte movements. Super efficient.
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 8:40 am     Reply with quote

It's kinda interesting what's in the manual......
all sorts of EASY ways to get PICs to perform....
be sure to look at the FAQ section too !!

Also worth looking at (need 2-3 cups of coffee ) are all the examples they put in, um, ah, the 'EXAMPLES' folder.....

I always have the manual open ( F11) cause I forget stuff real easy now that I'm nearing 70.
colin382



Joined: 03 Jun 2020
Posts: 37
Location: UK

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 9:00 am     Reply with quote

Thanks for the additional material. Like you I nearly always have the help file open. At 78 years old, it's even easier to forget.
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 12:47 pm     Reply with quote

It still seems ye ol' geezers forgot more than I'll ever know :-)
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 12:57 pm     Reply with quote

I know 113355 is a magical number for hungry folks......
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 1:04 pm     Reply with quote

Pi?
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 10, 2023 5:32 pm     Reply with quote

wow, good guess !!
magical number 113355
divide left 3 digits into the right 3 digits...
355/113 = Pi, good to 6 or 7 places.
It's the best integer approximation I've seen since the mid/late '50s..
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