TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A faster next-power-of-two algorithm

2 pointsby peripetylabsabout 12 years ago

1 comment

ColinWrightabout 12 years ago
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?
评论 #5692302 未加载
评论 #5692300 未加载