4 ms·
I noticed the following is in the middle of the article with no context that no one else is mentioning: unsigned average(unsigned a, unsigned b) {
by AnotherGoodName 5y ago
I noticed the following is in the middle of the article with no context that no one else is mentioning:
unsigned average(unsigned a, unsigned b)
{
return (a & b) + (a ^ b) / 2;
}
A quick sanity check of this
23 & 21 = 21
23 ^ 21 = 2
21 + 2 / 2 = 22 (order of operations)
I wonder why this is there. It seems the best solution but no one else is mentioning it. It also has no context near it. Nor is it stated correctly. It's just there on it's own.
- 829588225 5y ago23 ^ 21 = 2
- AnotherGoodName 5y agoSorry, edited the above. This is straight up right then which is weird. It's just there in the middle of the article with no context. In the middle of the SWAR method.
- shannongreen 5y agoIt is the SWAR method. Another comment explains it well, it basically treats each bit position as a 2-bit adder.
- jlynn 5y agoThe average of 23 and 21 is indeed 22.
- AnotherGoodName 5y agoOh right, sorry i'll edit this. It works straight up then. Weird it's there with no context.
- deleted 5y ago[deleted]