They've always peaked my interest, but never personally had to work with it, and just don't know the use cases where these become important!
They've always peaked my interest, but never personally had to work with it, and just don't know the use cases where these become important!
Packing data into bits is a space optimization. On modern processors, accessing memory is hideously slow compared to doing logical operations, so if you can pack more info into fewer bits, you get higher information density, and need to access main RAM less often. Thus, it speeds things up, sometimes dramatically.
Sorry, I don't have reading material per-se.
But just as an anecdotal example: I was using some of this trickery to implement a densely-packed Quadtree using Morton Codes. Morton Codes interleave the bits of the X and Y coordinates for a cell on a grid. So, in order to break a M-Code apart and combine them back together, you need to "select the even bits" or "select the odd bits", which can be done with some tricks like these.
(aside: it's "piqued" :)
I'm not exactly sure why, but I'm very intrigued nonetheless.
Aside: TIL, Thanks!
* some of Daniel Lemire's work in high performance data structures
* the blog posts related to solving board games on https://nullprogram.com
* the articles about board representations on the chess programming wiki https://www.chessprogramming.org/Main_Page
* Any explanation of Quake III's fast inverse square root
10000000
01000000
00100000
00010000
00001000
00000100
00000010
00000001
You could turn that into an array to animate an LED going from left-to-right or you could just write a simple function that bit-shifts your current state to the right.It's also useful in scrolling text: Imagine that 8x8 above contains a character like O. You could animate the O moving to the left by shifting the bits in each row to the left by one over and over again (according to whatever speed you've defined).
https://graphics.stanford.edu/~seander/bithacks.html
And if you like that, there's an entire book full of them, which is delightful:
https://www.amazon.ca/Hackers-Delight-2nd-Henry-Warren/dp/03...