Bitoperation OR vs addition

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 = 0A0B0C0D 0E0F0A0B
// 0E0F0A0B 00000000 | 00000000 0A0B0C0D
((result << 8) & 0xFF00) | (result >> 8);

So 0A0B0C0D 0E0F0A0B is 0E0F0A0B 0A0B0C0D afterwards.

But I also saw people using this:

((result << 8) & 0xFF00) + (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.


Source: c++

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.