Bit Hacking (with Go code)
lemire.me
lemire.me
It just highlights to me how the job of "programmer" can be many things, depending on your domain. One could argue that a goal of programming is to build higher abstractions so you don't need to manipulate bits in order to express yourself.
But that aside, I enjoy this stuff, so here are some links for others interested:
* A neat SO post about implementing "popcount" using SWAR trickery: https://stackoverflow.com/questions/109023/count-the-number-...
* Good collections of bit hacks:
** https://graphics.stanford.edu/~seander/bithacks.html (also mentioned in the article's comments)
Agreed, though I'd argue different languages entreat different kinds of knowledge. That's why there's value in having experience in many.
> One could argue that a goal of programming is to build higher abstractions so you don't need to manipulate bits in order to express yourself.
Could be; though I'd argue just because those abstractions are there, it's still good to know how and why they work. That reveals some things about the language which I think influences the subtle choices you make every day.
This feels natural now, but other languages inherited a decision dating back to B, when logical operators like && and|| didn't exist, and the bitwise forms were used in their stead.
With that having been said, it has exactly one downside in my opinion: when porting C code, you really have to be careful sometimes. C has 15 or so levels of operator precedence, splitting similar operators into their own precedence, whereas Go has only 5 and they're always left-associative. Particularly tricky C code may actually make use of this to reduce redundant parentheses in ways that may not be entirely obvious... (especially when combined with implicit casting.)
Whenever I do quick expression language parsers for any reason, I always follow Go's operator design. It's very easy to implement and other than not matching C behavior, has less footguns.