@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
66 lines (65 loc) • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @static
*/
var BitUtils = /** @class */ (function () {
function BitUtils() {
}
/**
* @deprecated not sure it has ever made sense to have it
* @returns a number in range ```[ 0 , 255 ]``` ( ```[ 0b0000_0000, 0b1111_1111 ]``` ) based on the first byte
*/
BitUtils.getFirstByte = function (bits) {
return Number("0x".concat(bits.toString(16).slice(0, 2)));
};
/**
* @deprecated use ```andMaskOfLength``` instead
*/
BitUtils.andMaskOfLengthInt = function (n) {
n = Math.round(Math.abs(n));
// operatons used are valid on singed int32
if (n >= 30) {
return BitUtils.andMaskOfLength(BigInt(n));
}
return BigInt((1 << n) - 1);
};
/**
* returns a ```bigint``` of that as the last ```n``` bits setted to ones;
*
* example
* ```ts
* BitUtils.getMaskOfLength( 7 ) === Bigint( 0b0111_1111 ); // true
* ```
*/
BitUtils.andMaskOfLength = function (n) {
return BigInt((BigInt(1)
<< n)
- BigInt(1));
};
BitUtils.getNLastBits = function (fromNuber, nBits) {
return (fromNuber & BitUtils.andMaskOfLength(nBits));
};
/**
* @returns the number of bits from the first setted to ```1``` on the left up until the end
*/
BitUtils.getNOfUsedBits = function (bits) {
if (bits === BigInt(0))
return 0;
return bits.toString(2).length;
};
BitUtils.minBytesRequired = function (bigint) {
if (bigint < BigInt(0))
throw new Error("BitUtils.minBytesRequired works for positives integers only");
var fullByteOnes = BigInt(255);
var mask = fullByteOnes;
var bytesRequired = 1;
while (bigint !== (bigint & mask)) {
mask = (mask << BigInt(8)) | fullByteOnes;
bytesRequired++;
}
return bytesRequired;
};
return BitUtils;
}());
exports.default = BitUtils;