agn-utils/.svn/pristine/dc/dcf362ca886adc2f890186ccf09c4b04fba63789.svn-base

32 lines
874 B
Plaintext

'use strict';
/**
* Extract n bits from a position
* @param numVal the number
* @param bits number of bits to extract
* @param from the bit postion right to left to extract. Zero index.
*/
function extractBits(numVal, bits, from) {
let _from = Math.max(0, from);
let _bits = Math.abs(bits);
return (((1 << _bits) - 1) & (numVal >> _from));
};
/**
* Check if k-th bit of a given number is set or not using right shift operator.
* @param {*} n the number value to check
* @param {*} at the bit position. Zero index.
*/
function isBitOn(n, bit) {
let _at = Math.max(0, bit);
return ((n >> _at) & 1);
};
function bytesToHex(bytes, delimeter = "") {
return bytes && Array.isArray(bytes) ? Array.from(
bytes,
byte => byte.toString(16).padStart(2, "0").toUpperCase()
).join(delimeter) : '';
}
module.exports = { extractBits, isBitOn, bytesToHex }