Created
September 14, 2017 10:18
-
-
Save ancyrweb/81e3382291492cf7044ea51a93136545 to your computer and use it in GitHub Desktop.
extractBits
This file contains hidden or 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
/** | |
* Extracts bits out of the decimal num from msb to lsb | |
* | |
* @param num | |
* @param msb the most significant bit | |
* @param lsb the least significant bit | |
* @returns {number} | |
*/ | |
function extractBits(num, msb, lsb) { | |
msb = msb || 0; | |
lsb = lsb || 8; | |
let msbCount = msb; | |
let mask = 255; | |
for(let pow = 7; msbCount > 0; msbCount--, pow--) { | |
mask -= Math.pow(2, pow); | |
} | |
return (num & mask) >> (8 - lsb); | |
} | |
module.exports = extractBits; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment