* 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)
| 20 | * If the highest bit is ``1``, the result will be negative. |
| 21 | */ |
| 22 | function 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 | } |
| 35 | exports.fromTwos = fromTwos; |
| 36 | /** |
| 37 | * Convert %%value%% to a twos-compliment representation of |