Demystifying bitwise operations, a gentle C tutorial
11–20 of 95 posts
Re: Demystifying bitwise operations, a gentle C tutorial
#12I don’t understand why bit masking and manipulation is so popular when it makes the code impossible to read. I like using Ruby, string, and pack and unpack.
Using a scripting language’s string packing is hundreds of times slower than doing a couple of bit operations, and if there’s a memory allocation involved, it can be thousands of times slower. If you’re curious about this, I recommend doing some profiling to find out how fast your favorite algorithms are when using C bitwise operators compared to Ruby string packing.
FWIW, having the code be readable is mostly a familiarity problem that you can resolve by practicing more bitwise operations, if you want. If you spend your days in Ruby, then there might not be strong reasons to, but if you’re curious and want to improve, you might have fun playing in C or C++. I spend my days mostly in CUDA, and using bit manipulation is par for the course, failure to use the best tricks can result in much lower compute throughput and much higher power consumption.
Re: Demystifying bitwise operations, a gentle C tutorial
#13Re: Demystifying bitwise operations, a gentle C tutorial
#14I don’t understand why bit masking and manipulation is so popular when it makes the code impossible to read. I like using Ruby, string, and pack and unpack.
https://github.com/ruby/ruby/blob/4ce642620f10ae18171b41e166...
Re: Demystifying bitwise operations, a gentle C tutorial
#15As the picture shows, we need 11 bits, with powers ranging from zero to ten, both included (under the implicit assumption that we want to represent all integers between 0 and 1078. If all we want to represent is 1078, we can do with one or, pedantically, zero bits)
Re: Demystifying bitwise operations, a gentle C tutorial
#16More such puzzles: http://www.cs.cmu.edu/afs/cs/academic/class/15213-f02/www/L1... and http://csapp.cs.cmu.edu/public/datalab.pdf
Bit Twiddling Hacks: http://graphics.stanford.edu/~seander/bithacks.html
Re: Demystifying bitwise operations, a gentle C tutorial
#17I thought this was going to be ridiculous but it's actually a really good. It's clear enough that you could extend it down to showing how logic gates work. The only thing missing is a little endian discussion; it assumes big endian (network byte order), and that may be confusing for all those x86 users out there.
It’s pretty much every user these days - even most MIPS based network oriented devices run LE. BE lost many years ago.
Re: Demystifying bitwise operations, a gentle C tutorial
#18I Can imagine in the past, this was “faster”, yet clang/gcc can emit the same by just writing a basic A/B function.
Seems the win goes to readability by reducing some of these old school hacks.
What say you, greybeards ?
Re: Demystifying bitwise operations, a gentle C tutorial
#19The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead.
But this is a static blog post, so it doesn't know how to adapt. Imagine the same post, but with some toggles/sliders (you can come up with even more sophisticated mechanisms): I indicate my level of competency, and the article re-writes itself to match my knowledge. Skip the boring math, show a lot of examples, etc. Or the opposite. The point is that it adapts to you. GPT (or some future variant) is really good at doing this.
Re: Demystifying bitwise operations, a gentle C tutorial
#20This long article is a great example of why I believe GPT has a strong future. The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead. But this is a s…
They have no place in software engineering then.