Isn't it better to do
(a>>1) + (b>>1) + (a&b&1)
No division needed. (a>>1) + (b>>1) + (a&b&1)
No division needed. unsigned average(unsigned a, unsigned b) {
return (a + b) / 2;
}
At the end of the day it's all just text. There are plenty of steps before any of it does anything at all.Really depends on where you're coming from. Anyone who has dipped their toes in embedded programming will immediately know they are equivalent, and many will correct /2 to a bitshift, because that's what you want to happen.
I get that bit twiddling is obscure outside of low level programming, but bit shifts really is kindergarten stuff in this domain.