I have byte array which holds 4 bytes to be converter to integer. I have these two examples, they both produce same result when tested on int.MinValue, 0, int.MaxValue and other values.
byte[] bytes; //holds 4 bytes
//solution a
int a = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
//solution b
return bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);
What are differences between these two examples, are they both valid to be used, might there be any execution speed difference? Unfortunately i were not able to find descriptions on the difference.
::Note that im looking answers for this specific case, please, instead of answering dont suggest BitConverter or other libraries.
Thanks