Skip to content

Instantly share code, notes, and snippets.

@luisalima
Created April 30, 2016 18:17
Show Gist options
  • Save luisalima/3f5d31f2ece58db22068cb7dfbff30d2 to your computer and use it in GitHub Desktop.
Save luisalima/3f5d31f2ece58db22068cb7dfbff30d2 to your computer and use it in GitHub Desktop.
Right and left shift operators
# 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