Can I Optimize this code with better array manipulation??

Hi everyone!!!!

I have a byte array of a certain size. I would like to grab it's last four bytes and put them next to each other to form a four byte hexadecimal value, in a variable. This way I can compare a four-byte CRC-32 value with my variable and check whether the CRC values are the same.
Now this is how I am doing this at the moment:

1
2
3
4
5
6
7


static unsigned long whole_four = 0; // This variable holds the last four bytes of the received bytes which are the CRC bytes

//Note that "MAX_INPUT" is the size of my array and "data" is just a pointer I have used as an argument to a function to point to the elements a buffer array.

whole_four = ((unsigned long)(*(data+(MAX_INPUT-4)))<< 24)  | ((unsigned long)(*(data+(MAX_INPUT-3)))<< 16) | ((unsigned long)(*(data+(MAX_INPUT-2)))<< 8) | ((unsigned long)(*(data+(MAX_INPUT-1))));


So as you can see I am shifting and "or"ing the the last four elements of my array to make a four byte variable.
Now, my question is:
Is there any faster way to achieve this and reduce the amount of processing power required?
I also like to mention that I am compiling this code on an Arduino Uno.
Any help or hints is much appreciated.
I don't think you can do much more from C, and what you've done seems ok.

Depending on how much you care, you could have the compiler save the assembler and see what it generates with different optimization levels.
Topic archived. No new replies allowed.