

Absolute Beginner's Guide to Bit Shifting - ksetyadi
http://stackoverflow.com/questions/141525/absolute-beginners-guide-to-bit-shifting

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

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

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

~~~
tmuir
As an embedded developer/EE, I feel the same way, but from the other side.
There is too much breadth for any single person to be proficient at all 7
layers, but from time to time, you still see job descriptions that ask for
assembly, C, C++, Java, PHP, HTML, Python, CSS, Flash, Haskell, PCB Layout,
VHDL, 7 yrs iOS...

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

~~~
prophetjohn
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, however, usually just involves a lookup table.

~~~
pm215
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 division by constant which
allows you to convert it into a series of 3 to 5 shift and add/subtract
operations; any decent C compiler will implement this. _Hacker's Delight_ has
the explanation of the algorithm, I think.

~~~
pjscott
Here's an article explaining the trick:

[http://ridiculousfish.com/blog/posts/labor-of-division-
episo...](http://ridiculousfish.com/blog/posts/labor-of-division-episode-
iii.html)

And a library from the same guy for generating code at runtime to do fast
division by constants:

<http://libdivide.com/>

------
swordswinger12
This could've helped me last week. I was pulling my hair out debugging a CUDA
implementation of the SHA3 candidate BLAKE, and after a full week of debugging
the same 70 lines (and one complete rewrite) the issue turned out to be most-
significant-bit padding with arithmetic right shifts. I just changed every
'char' type to 'uint8_t' and the code worked perfectly.

------
hackermom
In machine code language, we refer to these as rotating and shifting, instead
of "shifting and non-circular shifting".

