I'm confused by this. Both the algorithms mentioned seem to give the wrong result when N is a power of 2.<p>For example, N=4. Set all the lower bits to 1, giving 7. Increment, giving 8, return 8. But the power of two no less than 4 is 4.<p>Am I missing something?<p>Also, the alleged faster solution is only faster for larger numbers (on a log scale). Sure, there are more of them, but what if your function is called more often for small numbers than large ones?