Skip to content

Instantly share code, notes, and snippets.

@skyjur
Created February 5, 2019 06:28
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 skyjur/1d22605f1c7e6698a786f8ad01ea9316 to your computer and use it in GitHub Desktop.
Save skyjur/1d22605f1c7e6698a786f8ad01ea9316 to your computer and use it in GitHub Desktop.
bit slicing
// Binary bit slice from the right to the left
// msb: n-th more significant bit from the right (inclusive)
// lsb: n-th less signnificant bit from the right (inclusive)
//
// bitSlice(x, 2, 0) - last 3 bits of a number
// bitSlice(x, 3, 2) - 4th and 3rd least significant bits
function bitSlice(n, msb, lsb) {
return (n & ((1 << (msb + 1)) - 1)) >> lsb
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment