Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.
Absolute Beginner's Guide to Bit Shifting
11–20 of 23 posts
Re: Absolute Beginner's Guide to Bit Shifting
#12Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.
Are OS kernel, driver, and embedded systems development off topic on HN? There were several embedded developer posts in the latest Who's Hiring thread. Web development is by far the largest software segment that HN focuses on, but is by no means the only one.
I just wondered why it seems to considered something every CS grad/programmer should understand.
Re: Absolute Beginner's Guide to Bit Shifting
#13Earlier quoted context omitted.
In fact, bit shifting is always faster than anything. At the hardware level, it's just wiring; there is no logic involved in shifting the bits, so there is no propagation delay involved, unlike even the single logic gate operations like &, |, ~, etc. It's so much faster, that many compilers, for integer multiplication, will optimize these multiplications by converting them to shifts and adds. Integer division, howeve…
As well as the practical details of barrel shifter implementation design that psykotic points out, older CPUs couldn't afford the space for a barrel shifter and implemented shifts as a sequence of one-bit shifts. So for instance the 8086 took 8 clock cycles plus another 4 cycles per bit shift, and on that kind of CPU it was definitely not as fast as an addition. Incidentally there is a standard trick for integer divi…
http://ridiculousfish.com/blog/posts/labor-of-division-episo...
And a library from the same guy for generating code at runtime to do fast division by constants:
Re: Absolute Beginner's Guide to Bit Shifting
#14Earlier quoted context omitted.
Are OS kernel, driver, and embedded systems development off topic on HN? There were several embedded developer posts in the latest Who's Hiring thread. Web development is by far the largest software segment that HN focuses on, but is by no means the only one.
I never suggested it was off topic, I usually enjoy reading the low level programming threads on HN. I just wondered why it seems to considered something every CS grad/programmer should understand.
Re: Absolute Beginner's Guide to Bit Shifting
#15Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.
At CMU, three of the first four intro programming classes discuss bit shifting in depth.
Re: Absolute Beginner's Guide to Bit Shifting
#16Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.
int pixel = image.get(x, y);
int alphaVal (pixel & 0xFF000000) >> 24;
int redVal = (pixel & 0x00FF0000) >> 16;
int greenVal = (pixel & 0x0000FF00) >> 8;
int blueVal = (pixel & 0x000000FF);
That's just one example w/ one piece of software, but I know similar approaches are often used within the world of imaging / graphics. Maybe networking? Seem like it would correlate well to IP address operations.
Re: Absolute Beginner's Guide to Bit Shifting
#17Re: Absolute Beginner's Guide to Bit Shifting
#18Please don't read just the selected answer, because he got the optimizations wrong. Bit shifting operations are always faster than multiplications (as the guy with the second most voted answer explains).
> A good optimizing compiler will substitute shifts for multiplications when possible.
"substitute shifts for multiplications" means that multiplications will be replaced with shifts.
The wordier phrase would be "substitute with shifts for multiplications" or "substitute for multiplications with shifts".
Re: Absolute Beginner's Guide to Bit Shifting
#19Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.
In short, when you're dealing with a performance sensitive application they can be helpful to make it blazing fast. When you have a piece of code being executed many times they can be helpful since micro-optimizations start mattering then, too. For the same reason most of us don't write in assembly, most of us probably don't need it for our applications, we're free to waste, but besides being fun/interesting the practical applications where bit hacks can be beneficial do indeed go beyond your short-list. (Game engines (physics, graphics, AI, networking) and databases are two more general topics I can think of off the top of my head, compression is another but could just be a special case of databases.)
Re: Absolute Beginner's Guide to Bit Shifting
#20Beyond OS kernels , drivers and embedded systems is there any real use for this? I only ask because we were never taught about bit shifting at university and I can't think of a time where it would have ever been useful in my work but despite this it seems to be a very common thing to ask about at interviews so I have sort of educated myself about it for that reason alone.