dart:core
库中BigInt.toSigned
方法的用法介绍如下。
用法:
BigInt toSigned(
int width
)
返回此整数的最低有效 width
位,将保留的最高位扩展到符号。这与使用带符号的 2-s 补码表示截断值以适合 width
位相同。返回的值在所有高于 width
的位置具有相同的位值。
var big15 = BigInt.from(15);
var big16 = BigInt.from(16);
var big239 = BigInt.from(239);
// V--sign bit-V
big16.toSigned(5) == -big16; // 00010000 -> 11110000
big239.toSigned(5) == big15; // 11101111 -> 00001111
// ^ ^
此操作可用于从低级语言模拟算术。例如,要增加一个 8 位有符号数量:
q = (q + 1).toSigned(8);
q
将从 0
计数到 127
,换行到 -128
并重新计数到 127
。
如果输入值适合 width
位且没有截断,则结果与输入相同。避免截断 x
所需的最小宽度是 x.bitLength + 1
,即
x == x.toSigned(x.bitLength + 1);
相关用法
- Dart BigInt.toString用法及代码示例
- Dart BigInt.toRadixString用法及代码示例
- Dart BigInt.toDouble用法及代码示例
- Dart BigInt.toInt用法及代码示例
- Dart BigInt.toUnsigned用法及代码示例
- Dart BigInt.tryParse用法及代码示例
- Dart BigInt.operator_divide用法及代码示例
- Dart BigInt.from用法及代码示例
- Dart BigInt.isValidInt用法及代码示例
- Dart BigInt.parse用法及代码示例
- Dart BigInt.bitLength用法及代码示例
- Dart BigInt.gcd用法及代码示例
- Dart BigInt.operator_truncate_divide用法及代码示例
- Dart BigInt.compareTo用法及代码示例
- Dart BigInt.remainder用法及代码示例
- Dart BigInt.operator_modulo用法及代码示例
- Dart BigInt.pow用法及代码示例
- Dart BigInt用法及代码示例
- Dart Base64Encoder用法及代码示例
- Dart ByteData.view用法及代码示例
- Dart ByteData用法及代码示例
- Dart Base64Decoder用法及代码示例
- Dart MapMixin.containsKey用法及代码示例
- Dart Iterator用法及代码示例
- Dart AttributeClassSet.intersection用法及代码示例
注:本文由纯净天空筛选整理自dart.dev大神的英文原创作品 toSigned method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。