Skip to content

Commit

Permalink
Demonstrate wrapping with asIntN/asUintN Bigint static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe committed Aug 31, 2024
1 parent 62b9e8f commit cd19e90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
22 changes: 11 additions & 11 deletions live-examples/js-examples/bigint/bigint-asintn.js
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)
23 changes: 11 additions & 12 deletions live-examples/js-examples/bigint/bigint-asuintn.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const max = 2n ** 64n - 1n;
const U64_CEIL = 2n ** 64n;

Check failure on line 1 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'U64_CEIL' is assigned a value but never used

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));

Check failure on line 3 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'I64_CEIL' is not defined
// 18446744073709551615n (2n ** 64n - 1n, the maximum non-wrapping value)
console.log(BigInt.asUintN(64, I64_CEIL));

Check failure on line 5 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'I64_CEIL' is not defined
// 0n (wraps to zero)
console.log(BigInt.asUintN(64, I64_CEIL + 1n));

Check failure on line 7 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'I64_CEIL' is not defined
// 1n
console.log(BigInt.asUintN(64, I64_CEIL * 2n));

Check failure on line 9 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'I64_CEIL' is not defined
// 0n (wraps on multiples)
console.log(BigInt.asUintN(64, I64_CEIL * -42n));

Check failure on line 11 in live-examples/js-examples/bigint/bigint-asuintn.js

View workflow job for this annotation

GitHub Actions / build

'I64_CEIL' is not defined
// 0n (also wraps on negative multiples)

0 comments on commit cd19e90

Please sign in to comment.