Hacker News new | ask | show | jobs
by dheera 1587 days ago
Why not just

   (a >> 1) + (b >> 1) + (a & 1) & (b & 1)
2 comments

That's the "patented solution" referred to in the article.
Which is shocking to me, since this is the solution which jumps immediately to mind (and usually patents should be above that line). I could understand if the next solution was patented, but not this one.
Don't read the article, and you would never know it's patented.
"just"? That's 2 additions, 2 shifts, and 2-3 bitwise operations, while the other method is 1 addition, 1 shift, and 2 bitwise operations.
I would think optimizing compilers can optimize this expression to the one in the article, but I tried it on godbolt and they don't.