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 (2005)

136 pointsby memorableover 2 years ago

8 comments

thomover 2 years ago
My favourite bit of bit twiddling doesn&#x27;t seem to appear here - how to enumerate all the subsets of bits set. Like so many bit-twiddling hacks, I first saw this in chess engines:<p><a href="https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Traversing_Subsets_of_a_Set" rel="nofollow">https:&#x2F;&#x2F;www.chessprogramming.org&#x2F;Traversing_Subsets_of_a_Set</a>
评论 #33368023 未加载
评论 #33366962 未加载
评论 #33367206 未加载
评论 #33367358 未加载
评论 #33365099 未加载
raphlinusover 2 years ago
I love these kinds of things and use them in GPU programming, among other things. Things have changed in a variety of ways: population count and count-trailing-zeros are generally available as fast instructions now. Multiply is also now just as fast as other operations, so is not to be avoided.<p>A couple examples. [1] computes the sum of the number of bytes used of four consecutive segments of a bezier path - each segment can be lineto, quadto, or curveto, can be the end of a path or not, and be i16 or f32. 4 tag bytes are packed into a 32 bit word, it computes all these, then sums them together.<p>[2] linearizes a recursive subdivision into an iterative loop. The stack depth is represented as the number of zeros in a word, so pushing the stack is a left shift, and popping is a right shift. It turns out you want to pop multiple levels at once, and the number of levels is computed by countTrailingZeros. ([2] is experimental Rust code, but I will adapt this into a compute shader)<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;piet-gpu&#x2F;blob&#x2F;main&#x2F;piet-wgsl&#x2F;shader&#x2F;shared&#x2F;pathtag.wgsl#L48" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;piet-gpu&#x2F;blob&#x2F;main&#x2F;piet-wgsl&#x2F;s...</a><p>[2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;kurbo&#x2F;blob&#x2F;euler&#x2F;src&#x2F;euler.rs#L630" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;kurbo&#x2F;blob&#x2F;euler&#x2F;src&#x2F;euler.rs#...</a>
评论 #33366338 未加载
nullcover 2 years ago
If you like this, you&#x27;ll love the book &quot;Hacker&#x27;s Delight&quot;: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hacker%27s_Delight" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hacker%27s_Delight</a>
评论 #33364769 未加载
评论 #33367215 未加载
_448over 2 years ago
Here are two more(most of these websites link to each other):<p><a href="http:&#x2F;&#x2F;aggregate.org&#x2F;MAGIC&#x2F;" rel="nofollow">http:&#x2F;&#x2F;aggregate.org&#x2F;MAGIC&#x2F;</a><p><a href="https:&#x2F;&#x2F;www.inwap.com&#x2F;pdp10&#x2F;hbaker&#x2F;hakmem&#x2F;hakmem.html" rel="nofollow">https:&#x2F;&#x2F;www.inwap.com&#x2F;pdp10&#x2F;hbaker&#x2F;hakmem&#x2F;hakmem.html</a><p>I think this trend of publishing bit twiddling tricks started when Dr. Dobb&#x27;s Journal in it&#x27;s early days published an article about various such tricks. If someone has a link to that article then please could you share it? I think the article&#x27;s title was &quot;Bit Magic&quot;.<p>Edit:<p>There were two DDJ articles:<p>&quot;Binary Magic Numbers - Some Applications and Algorithms&quot; by Edwin E. Freed in Volume 8, Number 4, April, 1983; and<p>&quot;More on Binary Magic Numbers&quot; by Dale Wilson from Volume 9, Number 3, March, 1984.
评论 #33368172 未加载
jihadjihadover 2 years ago
Great page. A few years ago I saw it posted here on HN and bookmarked it, and would play around with a few of the (simpler) examples every now and then.<p>Last year I was in an interview and something around writing a program to find out if an integer is a power of 2 came up. I remembered the example from the doc above and used that (you basically AND your integer with itself minus one, which makes sense because any power of 2 is going to be 1 followed by some zeroes...see below). I got the job!<p><pre><code> 10000 &amp; 1111 ______ 1</code></pre>
评论 #33365934 未加载
评论 #33367327 未加载
评论 #33367234 未加载
评论 #33365638 未加载
ZoomZoomZoomover 2 years ago
For all interested in the topic , there&#x27;s a renowned classic of a cookbook: &quot;Matters Computational&quot; by Jörg Arndt.<p>Available freely: <a href="http:&#x2F;&#x2F;www.jjj.de&#x2F;fxt&#x2F;#fxtbook" rel="nofollow">http:&#x2F;&#x2F;www.jjj.de&#x2F;fxt&#x2F;#fxtbook</a>
trompover 2 years ago
In the game of Connect-4, bit twiddling allows amazing efficiency in board representation, board hashing, and win detection [1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;denkspuren&#x2F;BitboardC4&#x2F;blob&#x2F;master&#x2F;BitboardDesign.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;denkspuren&#x2F;BitboardC4&#x2F;blob&#x2F;master&#x2F;Bitboar...</a>
评论 #33369991 未加载
metadatover 2 years ago
A classic, this page continues to serve as a top-notch reference on obscure, historical programmer tricks.