Created
April 30, 2016 18:17
-
-
Save luisalima/3f5d31f2ece58db22068cb7dfbff30d2 to your computer and use it in GitHub Desktop.
Right and left shift operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# left shift operator is the equivalent of multiplying by a power of 2. | |
b = 1 | |
(b << 1).to_s(2) # "10", equivalent to multiplying by 2^1 | |
(b << 4).to_s(2) # "10000", equivalent to multiplying by 2^4 | |
# right shift operator is the equivalent to integer divisin by 2. | |
b = 32 | |
b.to_s(2) # "100000" | |
(b >> 1).to_s(2) # "10000", equivalent to dividing by 2 | |
(b>>3).to_s(2) # "100", equivalent to dividing by 2^3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment