-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate wrapping with asIntN/asUintN Bigint static methods
- Loading branch information
1 parent
62b9e8f
commit cd19e90
Showing
2 changed files
with
22 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
const max = 2n ** (64n - 1n) - 1n; | ||
const I64_CEIL = 2n ** 63n; | ||
|
||
function check64bit(number) { | ||
number > max | ||
? console.log("Number doesn't fit in signed 64-bit integer!") | ||
: console.log(BigInt.asIntN(64, number)); | ||
} | ||
|
||
check64bit(2n ** 64n); | ||
// Expected output: "Number doesn't fit in signed 64-bit integer!" | ||
check64bit(2n ** 32n); | ||
// Expected output: 4294967296n | ||
console.log(BigInt.asIntN(64, I64_CEIL - 1n)); | ||
// 9223372036854775807n (2n ** 64n - 1n, the maximum non-wrapping value) | ||
console.log(BigInt.asIntN(64, I64_CEIL)); | ||
// -9223372036854775808n (wraps to min value) | ||
console.log(BigInt.asIntN(64, I64_CEIL + 1n)); | ||
// -9223372036854775807n (min value + 1n) | ||
console.log(BigInt.asIntN(64, I64_CEIL * 2n)); | ||
// 0n (wrapped around to zero) | ||
console.log(BigInt.asIntN(64, -I64_CEIL * -42n)); | ||
// 0n (also wraps on negative multiples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
const max = 2n ** 64n - 1n; | ||
const U64_CEIL = 2n ** 64n; | ||
|
||
function check64bit(number) { | ||
number > max | ||
? console.log("Number doesn't fit in unsigned 64-bit integer!") | ||
: console.log(BigInt.asUintN(64, number)); | ||
} | ||
|
||
check64bit(2n ** 64n); | ||
// Expected output: "Number doesn't fit in unsigned 64-bit integer!" | ||
|
||
check64bit(2n ** 32n); | ||
// Expected output: 4294967296n | ||
console.log(BigInt.asUintN(64, I64_CEIL - 1n)); | ||
// 18446744073709551615n (2n ** 64n - 1n, the maximum non-wrapping value) | ||
console.log(BigInt.asUintN(64, I64_CEIL)); | ||
// 0n (wraps to zero) | ||
console.log(BigInt.asUintN(64, I64_CEIL + 1n)); | ||
// 1n | ||
console.log(BigInt.asUintN(64, I64_CEIL * 2n)); | ||
// 0n (wraps on multiples) | ||
console.log(BigInt.asUintN(64, I64_CEIL * -42n)); | ||
// 0n (also wraps on negative multiples) |