<< and >> is the same as multiply/divide by 2?
|
yes.
lets take a look.
<< is shift left in binary.
lets take the number 3 as an example:
11 (2+1 is 3)
shifted is 110
now that is 4+2+0 = 6. :P
and 110 shifted the other way... (hey look, the 2's position and the 1's position both doubled in value)
or one more with a visual, the powers of 2:
8 4 2 1 (2 to the 0 power is 1, by the way, 2 to the 1 power is 2, that is where the 1 comes from its 0,1,2...)
so you have a number
8 4 2 1 <- each value doubles.
0 0 1 1 <- the number 3
0 1 1 0 <- shifted, each value is worth twice as much because each value doubled first line
if you think about what shifting does and what binary means and what you learned about hex and decimal, it will all fall into place. Its the same thing. if you could (we don't have any hardware to do this) shift a base 10 number one position... 12 shifted is 120 ... which is the same as (multiplying by its base, which is 10 not 2 here).
I dunno how much of this is awe inspiring. This was taught along side logs and exponents as a basic need to know thing when I was in college because back then bit manipulation in code was extremely commonplace (for performance reasons, most of the time). The shift circuit was back when MUCH faster than the multiply circuit, so if you told it to shift, you were better off, if the multiply was by a power of 2 ... computers were so slow, this was critical knowledge. Its all cool the first time you see it, I guess. But this is or should be part of intro to computer science 101 formal material.