Created
February 5, 2019 06:28
-
-
Save skyjur/1d22605f1c7e6698a786f8ad01ea9316 to your computer and use it in GitHub Desktop.
bit slicing
This file contains 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
// 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