JavaScript 的 Number 采用 64 比特的 IEEE 754 标准来表示整数和浮点数数值,其中整数的安全范围在 -(253 - 1) 到 253 - 1 之间。换句话说,Node.js 既不能直接使用到 32 比特的整数,同时也无法使用 64 比特的整数。因此,如果要拿 Node.js 来做一些稍微复杂的计算,就需要撰写额外的程序来处理数据型态的部份,进而影响到运行性能。

int64-napi

int64-napi 是一个使用 Node.js 8 之后才支持的 N-API 所开发的模块,使用 Rust 语言原生的 i64 型态来直接进行 64 比特的整数运算,可以将性能的影响减到最低。

npmjs.com

npm 安装指令

npm install int64-napi

使用方法

首先,一个 64 比特的整数可以用以下几种不同的数据类型来创建:

  • 其它 Int64 的实体。
  • 一个 53 比特的整数。
  • 十进制数值的字符串。
  • 以 0x 开头的十六进制数值的字符串。
  • 以 0o 开头的八进制数值的字符串。
  • 以 0b 开头的二进制数值的字符串。
  • 一个存着 8 个字节数据的 Buffer。
静态函数的用法
Random
import { random } from "int64-napi";

const n = random(9876543210, "12345678901234"); // 5724595911391
Add
import { add } from "int64-napi";

const n = add("0x0000000000000001", 0x00000002); // 1 + 2 = 3
Subtract
import { subtract } from "int64-napi";

const n = subtract(1, 2); // 1 - 2 = -1
multiply
import { multiply } from "int64-napi";

const n = multiply(2, 6); // 2 * 6 = 12
divide
import { divide } from "int64-napi";

const n = divide(6, 4); // 6 / 4 = 1
mod
import { mod } from "int64-napi";

const n = mod(6, 4); // 6 % 4 = 2
shiftLeft
import { shiftLeft } from "int64-napi";

const n = shiftLeft(0b00101, 2); // 0b000101 << 2 = 0b010100
shiftRight
import { shiftRight } from "int64-napi";

const n1 = shiftRight(0b0101, 2); // 0b0101 >> 2 = 0b0001
const n2 = shiftRight(0b0110, 1); // 0b0110 >> 1 = 0b0011
const n3 = shiftRight("0b1111111111111111111111111111111111111111111111111111111111111011", 1); // 0b1111111111111111111111111111111111111111111111111111111111111011 >> 1 = 0b1111111111111111111111111111111111111111111111111111111111111101
shiftRightUnsigned
import { shiftRightUnsigned } from "int64-napi";

const n = shiftRightUnsigned("0b1111111111111111111111111111111111111111111111111111111111111011", 32); // 0b1111111111111111111111111111111111111111111111111111111111111011 >>> 32 = 0b0000000000000000000000000000000011111111111111111111111111111111
rotateRight
import { rotateRight } from "int64-napi";

const n = rotateRight("0x0000000080000100", 20); // 0x0010000000000800
rotateLeft
import { rotateLeft } from "int64-napi";

const n = rotateLeft("0x0010000000000800", 20); // 0x0000000080000100
and
import { and } from "int64-napi";

const n = and("0x000000000000FFFF", "0x0123456789ABCDEF"); // 0x000000000000CDEF
or
import { or } from "int64-napi";

const n = or("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000"); // 0xFFFFFFFFFFFFFFFF
xor
import { xor } from "int64-napi";

const n = xor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000"); // 0xFFFF0000FFFFFFFF
nand
import { nand } from "int64-napi";

const n = nand("0x000000000000FFFF", "0x0123456789ABCDEF"); // 0xFFFFFFFFFFFF3210
nor
import { nor } from "int64-napi";

const n = nor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000"); // 0x0000000000000000
xnor
import { xnor } from "int64-napi";

const n = xnor("0x0000FFFF0000FFFF", "0xFFFFFFFFFFFF0000"); // 0x0000FFFF00000000
not
import { not } from "int64-napi";

const n = not("0x0000FFFF0000FFFF"); // 0xFFFF0000FFFF0000
eq(等于)
import { eq } from "int64-napi";

const n = eq("0x0000FFFF0000FFFF", "281470681808895"); // true
ne(不等于)
import { ne } from "int64-napi";

const n = ne("0x0000FFFF0000FFFF", "0x0000FFFF00000000"); // true
gt(大于)
import { gt } from "int64-napi";

const n = gt("0x0000FFFF0000FFFF", "0x0000FFFF00000000"); // true
gte(大于等于)
import { gte } from "int64-napi";

const n = gte("0x0000FFFF0000FFFF", "0x0000FFFF00000000"); // true
lt(小于)
import { lt } from "int64-napi";

const n = lt("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF"); // false
lte(小于等于)
import { lte } from "int64-napi";

const n = lte("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF"); // true
comp(比较)

若第一个参数大于第二个参数,回传 1。
若第一个参数小于第二个参数,回传 -1。
若第一个参数等于第二个参数,回传 0。

import { comp } from "int64-napi";

const a = comp("0x0000FFFF0000FFFF", "0x0000FFFF0000FFFF"); // 0
const b = comp("0x0000FFFF0000FFFF", "0x0000FFFF00000000"); // 1
const c = comp("0x0000FFFF00000000", "0x0000FFFF0000FFFF"); // -1
对象方法的用法
创建对象实体
import { Int64 } from "int64-napi";

const i64 = new Int64(1);
对象方法

对象方法的种类和静态函数大致相同,请参考以下范例:

import { Int64 } from "int64-napi";

const i64 = new Int64(1);
const n1 = i64.add(1).multiply(3).subtract(3).divide(3).toDecimal(); // "1"

i64.set("0xFFFF000000000000");
const n2 = i64.shiftLeft(8).shiftRight(56).toHex(true); // "0xffffffffffffffff"

i64.set("0xFFFF000000000000");
const n3 = i64.shiftLeft(8).shiftRightUnsigned(56).toHex(true); // "0x00000000000000ff"

i64.set("0x000000010001");
const n4 = i64.rotateRight(8).toHex(true); // "0x0100000000000100"

i64.set("0x0000FFFFFFFF0000");
const n51 = i64.toHex(true); // "0x0000ffffffff0000"
const n52 = i64.toHex(); // "ffffffff0000"
const n53 = i64.toDecimal() + 1; // "2814749766451201"
const n54 = i64.toNumber() + 1; // 281474976645121

Int64 的对象是可变可被重复使用的,调用方法可能会导致它所表示的 64 比特整数值被改变。

要拷贝 Int64 的对象,可以这样写:

const i64_2 = i64.clone();