Int64
Use Rust to compute 64-bit signed integers.
You need to set up the Rust development environment: rustup
Usage
First of all, an int64(long) value can be represented by
- An Int64 object (instance).
- A 53-bit integer number.
- A string of a decimal number.
- A string of a hexadecimal number, starting with
0x
. - A string of an octal number, starting with
0o
. - A string of a binary number, starting with
0b
. - A buffer with 8 bytes
Static
Static funtions are used for quickly getting the final result as a JavaScript number. If you have several 64-bit integer computations to be done, it is better to use class methods instead.
A JavaScript number can safely represent only from -(2^53 - 1)
to 2^53 - 1
, and this library does not allow you to convert an unsafe-ranged integer to a JavaScript number.
Random
import { random } from "int64-napi";
const n = random(9876543210, "12345678901234");
Add
import { add } from "int64-napi";
const n = add("0x0000000000000001", 0x00000002);
Subtract
import { subtract } from "int64-napi";
const n = subtract(1, 2);
multiply
import { multiply } from "int64-napi";
const n = multiply(2, 6);
divide
import { divide } from "int64-napi";
const n = divide(6, 4);
mod
import { mod } from "int64-napi";
const n = mod(6, 4);
shiftLeft
import { shiftLeft } from "int64-napi";
const n = shiftLeft(0b00101, 2);
shiftRight
import { shiftRight } from "int64-napi";
const n1 = shiftRight(0b0101, 2);
const n2 = shiftRight(0b0110, 1);
const n3 = shiftRight("0b1111111111111111111111111111111111111111111111111111111111111011", 1);
shiftRightUnsigned
import { shiftRightUnsigned } from "int64-napi";
const n = shiftRightUnsigned("0b1111111111111111111111111111111111111111111111111111111111111011", 32);
rotateRight
import { rotateRight } from "int64-napi";
const n = rotateRight("0x0000000080000100", 20);
rotateLeft
import { rotateLeft } from "int64-napi";
const n = rotateLeft("0x0010000000000800", 20);
and
import { and } from "int64-napi";
const n = and("0x000000000000FFFF", "0x0123456789ABCDEF");
or
import { or } from "int64-napi";
const n = or("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000");
xor
import { xor } from "int64-napi";
const n = xor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000");
nand
import { nand } from "int64-napi";
const n = nand("0x000000000000FFFF", "0x0123456789ABCDEF");
nor
import { nor } from "int64-napi";
const n = nor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000");
xnor
import { xnor } from "int64-napi";
const n = xnor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000");
not
import { nor } from "int64-napi";
const n = nor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000");
eq (Equal)
import { eq } from "int64-napi";
const n = eq("0x0000FFFF0000FFFF", "281470681808895");
ne (Not Equal)
import { ne } from "int64-napi";
const n = ne("0x0000FFFF0000FFFF", "0x0000FFFF00000000");
gt (Greater Than)
import { gt } from "int64-napi";
const n = gt("0x0000FFFF0000FFFF", "0x0000FFFF00000000");
gte (Greater Than or Equal)
import { gte } from "int64-napi";
const n = gte("0x0000FFFF0000FFFF", "0x0000FFFF00000000");
lt (Less Than)
import { lt } from "int64-napi";
const n = lt("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF");
lte (Less Than or Equal)
import { lte } from "int64-napi";
const n = lte("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF");
comp (Compare)
If a < b
, returns -1
.
If a === b
, returns 0
.
If a > b
, returns 1
.
import { comp } from "int64-napi";
const a = comp("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF");
const b = comp("0x0000FFFF0000FFFF", "0x0000FFFF00000000");
const c = comp("0x0000FFFF00000000", "0x0000FFFF0000FFFF");
Instance / Object
Create an Instance
import { Int64 } from "int64-napi";
const i64 = new Int64(1);
Methods
Int64
instance has methods which are corresponding to static functions. Int64
instances are mutable and reusable, which means operations may modify its value.
import { Int64 } from "int64-napi";
const i64 = new Int64(1);
const n1 = i64.add(1).multiply(3).subtract(3).divide(3).toDecimal();
i64.set("0xFFFF000000000000");
const n2 = i64.shiftLeft(8).shiftRight(56).toHex(true);
i64.set("0xFFFF000000000000");
const n3 = i64.shiftLeft(8).shiftRightUnsigned(56).toHex(true);
i64.set("0x000000010001");
const n4 = i64.rotateRight(8).toHex(true);
i64.set("0x0000FFFFFFFF0000");
const n51 = i64.toHex(true);
const n52 = i64.toHex();
const n53 = i64.toDecimal() + 1;
const n54 = i64.toNumber() + 1;
To clone an Int64
instance.
const i64_2 = i64.clone();
License
MIT