I am reading an uint16 from a sensor connected to an raspberry (arm). I convert the data from little endian to big endian via:
// result = 0A 0B
// 0B 00 | 00 0A
(result << 8) | (result >> 8);
So 0A 0B is 0B 0A afterwards.
But I also saw people using this:
(result << 8) + (result >> 8);
Is there any advantage of using the addition? My guess is, there is no really advantage, it is just a bit slower.
There is a big difference when it comes to sum two numbers for example:
EF10 = 0FF0 + 00FF != 0FF0 | 00FF = 0FFF
Maybe I answered my own question already but it would be nice, if someone could evaluate. Would not be the first time I am tricking myself.
There is no difference (or advantage, at least not on modern processors) if the bits being combined aren't overlapping (in other words, the mask on the left-hand side has zeros for all bits set by the right-hand side, and vice versa)
The code shown here:
is a little confusing - the comment seems to imply that it's a 32-bit value, but the calculation implies a 16-bit
result
You will get different results for example if you do something like this:
vs
when
value
already has the bit 7 set.Note that at least in clang:
The exact same code is generated:
Using bitwise or
|
makes it clear what is the intention to who reads the code, and that's the important point.I would be surprised of speed differences between the two approaches.
Even better would be:
because there is no need mask the left part (as incoming bits are zeros).
In case of a signed integer instead what is coming in when performing a right-shift operation on negative values is implementation dependent, therefore portable code should use
to make it clear that a byte-swap operation is requested.