Skip to content

Instantly share code, notes, and snippets.

@mortenpi
Created September 28, 2015 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortenpi/2cc3257dc887fa5e6583 to your computer and use it in GitHub Desktop.
Save mortenpi/2cc3257dc887fa5e6583 to your computer and use it in GitHub Desktop.
Get the nth bit of a number in Julia.
# Gets the Nth bit of m
function bit(N, m)
m & (1<<(N-1)) >> (N-1)
end
@show [bit(n,0) for n=1:5]
@show [bit(n,1) for n=1:5]
@show [bit(n,2) for n=1:5]
@show [bit(n,3) for n=1:5]
@show [bit(n,5) for n=1:5]
@show [bit(n,15) for n=1:5]
@show [bit(n,16) for n=1:5]
@show [bit(n,31) for n=1:5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment