Dart BigInt toUnsigned()
Syntax & Examples
BigInt.toUnsigned() method
The `toUnsigned()` method of BigInt class returns the least significant `width` bits of this BigInt as a non-negative number.
Syntax of BigInt.toUnsigned()
The syntax of BigInt.toUnsigned() method is:
BigInt toUnsigned(int width)
This toUnsigned() method of BigInt returns the least significant width
bits of this big integer as a non-negative number (i.e. unsigned representation). The returned value has zeros in all bit positions higher than width
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
width | required | the number of bits to retain |
Return Type
BigInt.toUnsigned() returns value of type BigInt
.
✐ Examples
1 Convert BigInt to unsigned value with 64 bits
In this example,
- We create a BigInt
bigInt
with a large value. - We use the
toUnsigned()
method to convert it to an unsigned value with 64 bits. - We print the unsigned value to standard output.
Dart Program
void main() {
BigInt bigInt = BigInt.parse('-12345678901234567890');
BigInt unsignedValue = bigInt.toUnsigned(64);
print('Unsigned value with 64 bits: $unsignedValue');
}
Output
Unsigned value with 64 bits: 6101065172474983726
2 Convert BigInt to unsigned value with 32 bits
In this example,
- We create a BigInt
bigInt
with a large negative value. - We use the
toUnsigned()
method to convert it to an unsigned value with 32 bits. - We print the unsigned value to standard output.
Dart Program
void main() {
BigInt bigInt = BigInt.parse('-98765432109876543210');
BigInt unsignedValue = bigInt.toUnsigned(32);
print('Unsigned value with 32 bits: $unsignedValue');
}
Output
Unsigned value with 32 bits: 450461974
Summary
In this Dart tutorial, we learned about toUnsigned() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.