Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Last active October 20, 2020 05:48
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 EmmanuelOga/e8c15937a03f71ea1f86a0e5c5a9cbcf to your computer and use it in GitHub Desktop.
Save EmmanuelOga/e8c15937a03f71ea1f86a0e5c5a9cbcf to your computer and use it in GitHub Desktop.
Return least and most significant bits of a number.
function lsbMsb(bits) {
if (!bits) return {lsb: undefined, msb: undefined};
let lsb = 0;
for (; lsb < 31 && (bits & 1) === 0; bits >>= 1) {
lsb++;
}
let msb = lsb;
for (bits >>= 1; msb < 31 && bits !== 0; bits >>= 1) {
msb++;
}
return {lsb, msb};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment