News for hackers. Stuff that matters.
Re:EABitTricks.h by erksa on Monday December 19, 2022 @01:02PM (#34055275)
Does anyone have any reading material on introduction to where bit-wise programming becomes important knowledge? Is it mostly just used in game-dev?

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!

Re:EABitTricks.h by thoughtFrame on Monday December 19, 2022 @01:22PM (#34055594)
I don't know of any formal introductions to the topic but you can see them in action in these places too

* 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

Re:EABitTricks.h by erksa on Monday December 19, 2022 @03:24PM (#34057158)
Thanks for the resources, these are great! As an avid chess fan I've always played around with the idea of trying making a basic chess engine.. This might just be the push!!
Re:EABitTricks.h by _0ffh on Monday December 19, 2022 @01:13PM (#34055447)
Most obvious use case I can think of is embedded programming, e.g. when you're setting control registers.
Re:EABitTricks.h by ianlevesque on Monday December 19, 2022 @01:12PM (#34055434)
Interacting with hardware registers, such as in a device driver, is another common use case.
Re:EABitTricks.h by woodrowbarlow on Monday December 19, 2022 @01:51PM (#34055987)
or implementing file formats / network protocols
Re:EABitTricks.h by xxpor on Monday December 19, 2022 @01:53PM (#34056023)
When dealing with networking it's extremely common.
Re:EABitTricks.h by riskable on Monday December 19, 2022 @02:11PM (#34056246)
In embedded programming it can be useful to control an animation on an LED matrix since a lot of LED drivers and multiplexers enable pins based on the bits passed through their respective drivers. Example of how you might animate 8 LED connected to 8 pins:

    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).

Re:EABitTricks.h by erksa on Monday December 19, 2022 @03:22PM (#34057139)
Thanks man, this was a very clear explanation!
Re:EABitTricks.h by anonymoushn on Monday December 19, 2022 @02:25PM (#34056432)
You end up needing this stuff to write fast parsers of text formats, but you need a bunch of other stuff too, and there isn't really any introductory material online.
Re:EABitTricks.h by rickstanley on Monday December 19, 2022 @02:47PM (#34056707)
Maybe what danbruc commented [0] may be of interest for you.

[0]: https://news.ycombinator.com/item?id=34055831

Re:EABitTricks.h by erksa on Monday December 19, 2022 @03:17PM (#34057080)
Thanks!
Re:EABitTricks.h by jesse__ on Monday December 19, 2022 @02:56PM (#34056810)
Not an answer to your exact question, but there's:

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...

Re:EABitTricks.h by turtledragonfly on Monday December 19, 2022 @03:10PM (#34056977)
The general use-case is "high-performance and/or embedded systems." Games, and especially console games, happen to check both of those boxes. Another high-perf case is financial systems (eg: high-frequency trading). Other embedded systems would include things like robotics, automotive software, audio synthesizers and processing, etc.

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" :)

Re:EABitTricks.h by erksa on Monday December 19, 2022 @03:21PM (#34057133)
Thank you for the thorough break down. All the replies have immediately framed my "understanding" much more.

I'm not exactly sure why, but I'm very intrigued nonetheless.

Aside: TIL, Thanks!

Re:EABitTricks.h by teawrecks on Monday December 19, 2022 @03:28PM (#34057205)
Anywhere that a byte/word/dword is used as a first class data structure, there is likely some operation on the data structure that can be accomplished with a bit trick. As others have said, this is most often the case where CPU cycles are tightly constrained.
Re:EABitTricks.h by zamalek on Tuesday December 20, 2022 @07:04AM (#34064748)
If you're writing a standard library from scratch, they can be important in data structure or algorithms that are expected to be in hot loops.
Re:EABitTricks.h by IYasha on Tuesday December 20, 2022 @12:43PM (#34069352)
NETWORKS. All use cases apply.