MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / fromTwos

Function fromTwos

lib.commonjs/utils/maths.js:22–34  ·  view source on GitHub ↗

* Convert %%value%% from a twos-compliment representation of %%width%% * bits to its value. * * If the highest bit is ``1``, the result will be negative.

(_value, _width)

Source from the content-addressed store, hash-verified

20 * If the highest bit is ``1``, the result will be negative.
21 */
22function fromTwos(_value, _width) {
23 const value = getUint(_value, "value");
24 const width = BigInt(getNumber(_width, "width"));
25 (0, errors_js_1.assert)((value >> width) === BN_0, "overflow", "NUMERIC_FAULT", {
26 operation: "fromTwos", fault: "overflow", value: _value
27 });
28 // Top bit set; treat as a negative value
29 if (value >> (width - BN_1)) {
30 const mask = (BN_1 << width) - BN_1;
31 return -(((~value) & mask) + BN_1);
32 }
33 return value;
34}
35exports.fromTwos = fromTwos;
36/**
37 * Convert %%value%% to a twos-compliment representation of

Callers

nothing calls this directly

Calls 2

getUintFunction · 0.70
getNumberFunction · 0.70

Tested by

no test coverage detected