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.

Counting set bits in an interesting way

63 pointsby robalniabout 3 years ago

7 comments

Const-meabout 3 years ago
For production code as opposed to exams of job interviews, it’s usually better to use hardware implementation. All modern CPUs have instructions for that, popcnt on Intel&#x2F;AMD, vcnt.8 on ARM Neon.<p>Many languages have standard library functions, or hardware intrinsics, to emit these instructions: std::popcount in C++&#x2F;20, _popcnt32 and _popcnt64 intrinsics for Intel&#x2F;AMD, __builtin_popcount in gcc&#x2F;clang, BitOperations.PopCount in C#, etc.
评论 #31218690 未加载
评论 #31218358 未加载
OskarSabout 3 years ago
I&#x27;m quite fond of this classic for popcount [1]:<p><pre><code> int popcnt(unsigned int n) { int p = 0; while (n) { p++; n &amp;= n-1; } return p; } </code></pre> But it has a branch in it, so I don&#x27;t know if it&#x27;s competitive with the &quot;simple&quot; version of just counting the ones or this version, even though the loop should run fewer iterations. Obviously the real answer is to just use the compiler intrinsics for this, but what fun is that?<p>[1]: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;b9rK3sYah" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;b9rK3sYah</a>
评论 #31216289 未加载
评论 #31215691 未加载
评论 #31215939 未加载
评论 #31217163 未加载
trompabout 3 years ago
The way I see it as working, is that the i&#x27;th bit in x is initially added to the i&#x27;th bit in diff, and subsequently subtracted from the (i-1)th, the (i-2)th, ... the 0th bit of diff.<p>So this bit of x, if set, contributes (1&lt;&lt;i) - (1&lt;&lt;(i-1) + 1&lt;&lt;(i-2) + ... + 1&lt;&lt;0) = 1 to diff altogether.
评论 #31215489 未加载
fanf2about 3 years ago
There is a chapter by Hank Warren all about popcount in the Beautiful Code book, <a href="https:&#x2F;&#x2F;www.oreilly.com&#x2F;library&#x2F;view&#x2F;beautiful-code&#x2F;9780596510046&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.oreilly.com&#x2F;library&#x2F;view&#x2F;beautiful-code&#x2F;97805965...</a> and there is much more along similar lines in Warren’s book Hacker’s Delight <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hacker&#x27;s_Delight" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hacker&#x27;s_Delight</a><p>My favourite use of popcount is for packing sparse vectors.
评论 #31216772 未加载
mmphosisabout 3 years ago
; <a href="http:&#x2F;&#x2F;forum.6502.org&#x2F;viewtopic.php?t=1206" rel="nofollow">http:&#x2F;&#x2F;forum.6502.org&#x2F;viewtopic.php?t=1206</a><p><pre><code> LDX #$00 ; clear bit count loop ASL ; shift a bit BCC skip ; did one shift out? INX ; add one to count skip BNE loop ; repeat till zero RTS</code></pre>
评论 #31218889 未加载
svnpennabout 3 years ago
&gt; while (x)<p>I never realized how much I hate this style of code until I started using Go. Go only allows Boolean conditions, so you have to do this:<p>&gt; while (x &gt;= 1)<p>Yeah, it&#x27;s more code, but it&#x27;s more readable too.
评论 #31217665 未加载
评论 #31218931 未加载
评论 #31218487 未加载
inetseeabout 3 years ago
I had this as an interview question years ago. I just used a table lookup. It&#x27;s not compact, but it is fast.
评论 #31218144 未加载
评论 #31218082 未加载
评论 #31219440 未加载