I was reading somewhere that it is faster to use bitwise operators instead of if statements where possible. I am working on an image processing project and I have various methods for doing math on pixels. For instance when I add a pixel I would check and make sure the sum does not go over the maximum value. I changed it to be this...
Pixel16 operator+(Pixel16 p) const noexcept
{
uint_fast32_t r = red + p.red;
uint_fast32_t g = green + p.green;
uint_fast32_t b = blue + p.blue;
return Pixel16(r | -(r > 0xffff), g | -(g > 0xffff), b | -(b > 0xffff));
}
Do you guys think it is faster than writing statements like...
if(r > 0xffff)
r = 0xffff;
FYI red, green, and blue are member variables of type uint16_t