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.

Bit Twiddling Hacks

29 pointsby rguldenerover 12 years ago

7 comments

DanielRibeiroover 12 years ago
Former conversations:<p><a href="http://news.ycombinator.com/item?id=2570269" rel="nofollow">http://news.ycombinator.com/item?id=2570269</a><p><a href="http://news.ycombinator.com/item?id=3452408" rel="nofollow">http://news.ycombinator.com/item?id=3452408</a><p><a href="http://news.ycombinator.com/item?id=513935" rel="nofollow">http://news.ycombinator.com/item?id=513935</a><p><a href="http://news.ycombinator.com/item?id=2630593" rel="nofollow">http://news.ycombinator.com/item?id=2630593</a><p><a href="http://news.ycombinator.com/item?id=4590134" rel="nofollow">http://news.ycombinator.com/item?id=4590134</a><p><a href="http://news.ycombinator.com/item?id=86419" rel="nofollow">http://news.ycombinator.com/item?id=86419</a>
pandamanover 12 years ago
It's a little known fact that &#62;&#62; in C++ (and I suspect in C as well) on a signed type is not guaranteed to be an arithmetical shift. It's up to the implementation and most implementation choose arithmetical shift for signed and logical shift for unsigned but this does not mean that <i>all</i> the implementations do this. So some of this tricks are contingent on the particular implementation specific behavior.
simiasover 12 years ago
An oldie but a goodie.<p>Mandatory warning: please don't use those unless you really know what you're doing. Most modern CPUs have instructions for computing all of that stuff and the compiler will try to use them. Those clever hacks might actually hurt both readability and performance if they're misused.<p>Typically on PC architectures (and even on most modern embedded systems) you shouldn't have to use those hacks.
评论 #4591681 未加载
评论 #4591236 未加载
halayliover 12 years ago
On a similar note:<p><a href="http://www.amazon.com/gp/product/0321842685/ref=s9_simh_gw_p14_d0_i1?pf_rd_m=ATVPDKIKX0DER&#38;pf_rd_s=center-2&#38;pf_rd_r=1QMBXBDPHW9X6ZJ1KDN9&#38;pf_rd_t=101&#38;pf_rd_p=1389517282&#38;pf_rd_i=507846" rel="nofollow">http://www.amazon.com/gp/product/0321842685/ref=s9_simh_gw_p...</a>
评论 #4591556 未加载
zxcdwover 12 years ago
I think the real value of these hacks is to <i>understand how and they work</i> rather than the possible efficiency gains they may give. Sure, sometimes you just <i>have to</i> get the gains, but these days, on modern platforms this is hardly ever the case.<p>But man, if you <i>think</i> you know how to deal with the binary and bitwise operations, please take some time to go through all of the hacks and examine how they work. It's quite possible you learn something new and interesting, which may have surprising applications elsewhere.
jweckerover 12 years ago
Classic compilation. Always check for cpu builtin functions (rtfm gcc) first though when you need this kind of optimization "in real life."
csmattover 12 years ago
Strange, I was using this the other day for the TI Launchpad-based project I'm working on.