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 Hacking (with Go code)

111 pointsby gus_leonelalmost 2 years ago

7 comments

interroboinkalmost 2 years ago
The intro sentence, &quot;At a fundamental level, a programmer needs to manipulate bits,&quot; gave me a strange moment of mental dissonance.<p>It just highlights to me how the job of &quot;programmer&quot; can be many things, depending on your domain. One could argue that a goal of programming is to build higher abstractions so you <i>don&#x27;t</i> need to manipulate bits in order to express yourself.<p>But that aside, I enjoy this stuff, so here are some links for others interested:<p>* A neat SO post about implementing &quot;popcount&quot; using SWAR trickery: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;109023&#x2F;count-the-number-of-set-bits-in-a-32-bit-integer&#x2F;109025#109025" rel="nofollow noreferrer">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;109023&#x2F;count-the-number-...</a><p>* Good collections of bit hacks:<p>** <a href="https:&#x2F;&#x2F;graphics.stanford.edu&#x2F;~seander&#x2F;bithacks.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;graphics.stanford.edu&#x2F;~seander&#x2F;bithacks.html</a> (also mentioned in the article&#x27;s comments)<p>** <a href="http:&#x2F;&#x2F;aggregate.org&#x2F;MAGIC&#x2F;" rel="nofollow noreferrer">http:&#x2F;&#x2F;aggregate.org&#x2F;MAGIC&#x2F;</a>
评论 #36404154 未加载
评论 #36399931 未加载
jchwalmost 2 years ago
Go doesn&#x27;t differ a ton from other programming languages but there are definitely a couple of things. I think when it comes to bit manipulation, my favorite detail about Go is the built-in AND NOT operator, which also has a corresponding compound assignment operator. It&#x27;s mentioned in here, but I think it&#x27;s worth calling out: not all languages have it, but it&#x27;s a good addition.
评论 #36405069 未加载
ungerikalmost 2 years ago
Old guy here: funny how manipulating bits is worth a HN post nowadays :-D
评论 #36400872 未加载
zelphirkaltalmost 2 years ago
Interesting! Only yesterday I had to get back into the bits (actually treating integera as bits, using shift operations and masks and bitwise-and), for the purpose of implementing base64 encoding. Good to know how to do this kind of stuff.
bryancoxwellalmost 2 years ago
This is a fun post. Simplifying a problem to a series go bitwise operations always feels strangely satisfying to me.
ww520almost 2 years ago
Bit tweaking is always fun. I have the need to implement a long bit array spanning multiple words recently and it gets surprisingly non-trivial quickly, especially dealing with subranges or searching for the next 0 or 1 bit from a starting position. But it&#x27;s still loads of fun.
shaftoe444almost 2 years ago
Implementing a bitset[0] in Go is a fun exercise if this article interested you.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_array" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bit_array</a>