(_value, _width)
| 17 | * If the highest bit is ``1``, the result will be negative. |
| 18 | */ |
| 19 | export 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. |
no test coverage detected