5 ms·
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 al
by erksa 4y ago
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!
- ianlevesque 4y agoInteracting with hardware registers, such as in a device driver, is another common use case.
- woodrowbarlow 4y agoor implementing file formats / network protocols
- _0ffh 4y agoMost obvious use case I can think of is embedded programming, e.g. when you're setting control registers.
- thoughtFrame 4y agoI 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 https://nullprogram.com * the articles about board representations on the chess programming wiki https://www.chessprogramming.org/Main_Page https://www.chessprogramming.org/Main_Page * Any explanation of Quake III's fast inverse square root
- erksa 4y agoThanks 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!!
- xxpor 4y agoWhen dealing with networking it's extremely common.
- riskable 4y agoIn 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).
- erksa 4y agoThanks man, this was a very clear explanation!
- anonymoushn 4y agoYou 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.
- rickstanley 4y agoMaybe what danbruc commented [0] may be of interest for you. [0]: https://news.ycombinator.com/item?id=34055831 https://news.ycombinator.com/item?id=34055831
- erksa 4y agoThanks!
- jesse__ 4y agoNot an answer to your exact question, but there's: https://graphics.stanford.edu/~seander/bithacks.html 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/0321842685 https://www.amazon.ca/Hackers-Delight-2nd-Henry-Warren/dp/03...
- turtledragonfly 4y agoThe 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" :)
- erksa 4y agoThank 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!
- teawrecks 4y agoAnywhere 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.
- zamalek 4y agoIf 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.
- IYasha 4y agoNETWORKS. All use cases apply.