I’ve never used the >> and << operators, not because I’ve never needed them, but because I don’t know if I could have used them, or where I should have.
100 >> 3 outputs 12 instead of 12.5. Why is this. Perhaps learning where to best use right shift will answer that implicitly, but I’m curious.
Right shift is not division
Let’s look at what right-shift actually does, and it will become clear.
First, recall that a number is stored in memory as a collection of binary digits. If we have 8 bits of memory, we can store 2 as
00000010and 5 as00000101.Right-shift takes those digits and shifts them to the right. For example, right-shifting our above two digits by one will give
00000001and00000010respectively.Notice that the lowest digit (right-most) is shifted off the end entirely and has no effect on the final result.