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

Function fromTwos

lib.esm/utils/maths.js:19–31  ·  view source on GitHub ↗
(_value, _width)

Source from the content-addressed store, hash-verified

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

Callers 8

checkValueFunction · 0.90
fromBytesMethod · 0.90
decodeMethod · 0.90
checkValueFunction · 0.90
fromBytesMethod · 0.90
decodeMethod · 0.90

Calls 3

assertFunction · 0.90
getUintFunction · 0.70
getNumberFunction · 0.70

Tested by

no test coverage detected