@homebridge/long v5.2.1
long.js
A Long class for representing a 64 bit two's-complement integer value derived from the Closure Library for stand-alone use and extended with unsigned support.
Fork Status
This fork is maintained to work around a crash on ARMv6 platforms running node 16.1+.
See long.js/107.
Background
As of ECMA-262 5th Edition, "all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type", which is "representing the doubleprecision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic". The maximum safe integer in JavaScript is 253-1.
Example: 264-1 is 18446744073709551615 but in JavaScript it evaluates to 18446744073709552000.
Furthermore, bitwise operators in JavaScript "deal only with integers in the range −231 through 231−1, inclusive, or in the range 0 through 232−1, inclusive. These operators accept any value of the Number type but first convert each such value to one of 232 integer values."
In some use cases, however, it is required to be able to reliably work with and perform bitwise operations on the full 64 bits. This is where long.js comes into play.
Usage
The package exports an ECMAScript module with an UMD fallback.
$> npm install longimport Long from "long";
var value = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(value.toString());
...Note that mixing ESM and CommonJS is not recommended as it yields different classes, albeit with the same functionality.
Usage with a CDN
- From GitHub via jsDelivr:
https://cdn.jsdelivr.net/gh/dcodeIO/long.js@TAG/index.js(ESM) - From npm via jsDelivr:
https://cdn.jsdelivr.net/npm/long@VERSION/index.js(ESM)https://cdn.jsdelivr.net/npm/long@VERSION/umd/index.js(UMD) From npm via unpkg:
https://unpkg.com/long@VERSION/index.js(ESM)https://unpkg.com/long@VERSION/umd/index.js(UMD)Replace
TAGrespectivelyVERSIONwith a specific version or omit it (not recommended in production) to use main/latest.
API
Constructor
- new Long(low:
number, high?:number, unsigned?:boolean) Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as signed integers. See the from* functions below for more convenient ways of constructing Longs.
Fields
Long#low:
numberThe low 32 bits as a signed value.Long#high:
numberThe high 32 bits as a signed value.Long#unsigned:
booleanWhether unsigned or not.
Constants
Long.ZERO:
LongSigned zero.Long.ONE:
LongSigned one.Long.NEG_ONE:
LongSigned negative one.Long.UZERO:
LongUnsigned zero.Long.UONE:
LongUnsigned one.Long.MAX_VALUE:
LongMaximum signed value.Long.MIN_VALUE:
LongMinimum signed value.Long.MAX_UNSIGNED_VALUE:
LongMaximum unsigned value.
Utility
Long.isLong(obj:
*):booleanTests if the specified object is a Long.Long.fromBits(lowBits:
number, highBits:number, unsigned?:boolean):LongReturns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.Long.fromBytes(bytes:
number[], unsigned?:boolean, le?:boolean):LongCreates a Long from its byte representation.Long.fromBytesLE(bytes:
number[], unsigned?:boolean):LongCreates a Long from its little endian byte representation.Long.fromBytesBE(bytes:
number[], unsigned?:boolean):LongCreates a Long from its big endian byte representation.Long.fromInt(value:
number, unsigned?:boolean):LongReturns a Long representing the given 32 bit integer value.Long.fromNumber(value:
number, unsigned?:boolean):LongReturns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.Long.fromString(str:
string, unsigned?:boolean, radix?:number) Long.fromString(str:string, radix:number) Returns a Long representation of the given string, written using the specified radix.Long.fromValue(val:
*, unsigned?:boolean):LongConverts the specified value to a Long using the appropriate from* function for its type.
Methods
Long#add(addend:
Long | number | string):LongReturns the sum of this and the specified Long.Long#and(other:
Long | number | string):LongReturns the bitwise AND of this Long and the specified.Long#compare/comp(other:
Long | number | string):numberCompares this Long's value with the specified's. Returns0if they are the same,1if the this is greater and-1if the given one is greater.Long#divide/div(divisor:
Long | number | string):LongReturns this Long divided by the specified.Long#equals/eq(other:
Long | number | string):booleanTests if this Long's value equals the specified's.Long#getHighBits():
numberGets the high 32 bits as a signed integer.Long#getHighBitsUnsigned():
numberGets the high 32 bits as an unsigned integer.Long#getLowBits():
numberGets the low 32 bits as a signed integer.Long#getLowBitsUnsigned():
numberGets the low 32 bits as an unsigned integer.Long#getNumBitsAbs():
numberGets the number of bits needed to represent the absolute value of this Long.Long#greaterThan/gt(other:
Long | number | string):booleanTests if this Long's value is greater than the specified's.Long#greaterThanOrEqual/gte/ge(other:
Long | number | string):booleanTests if this Long's value is greater than or equal the specified's.Long#isEven():
booleanTests if this Long's value is even.Long#isNegative():
booleanTests if this Long's value is negative.Long#isOdd():
booleanTests if this Long's value is odd.Long#isPositive():
booleanTests if this Long's value is positive or zero.Long#isZero/eqz():
booleanTests if this Long's value equals zero.Long#lessThan/lt(other:
Long | number | string):booleanTests if this Long's value is less than the specified's.Long#lessThanOrEqual/lte/le(other:
Long | number | string):booleanTests if this Long's value is less than or equal the specified's.Long#modulo/mod/rem(divisor:
Long | number | string):LongReturns this Long modulo the specified.Long#multiply/mul(multiplier:
Long | number | string):LongReturns the product of this and the specified Long.Long#negate/neg():
LongNegates this Long's value.Long#not():
LongReturns the bitwise NOT of this Long.Long#countLeadingZeros/clz():
numberReturns count leading zeros of this Long.Long#countTrailingZeros/ctz():
numberReturns count trailing zeros of this Long.Long#notEquals/neq/ne(other:
Long | number | string):booleanTests if this Long's value differs from the specified's.Long#or(other:
Long | number | string):LongReturns the bitwise OR of this Long and the specified.Long#shiftLeft/shl(numBits:
Long | number | string):LongReturns this Long with bits shifted to the left by the given amount.Long#shiftRight/shr(numBits:
Long | number | string):LongReturns this Long with bits arithmetically shifted to the right by the given amount.Long#shiftRightUnsigned/shru/shr_u(numBits:
Long | number | string):LongReturns this Long with bits logically shifted to the right by the given amount.Long#rotateLeft/rotl(numBits:
Long | number | string):LongReturns this Long with bits rotated to the left by the given amount.Long#rotateRight/rotr(numBits:
Long | number | string):LongReturns this Long with bits rotated to the right by the given amount.Long#subtract/sub(subtrahend:
Long | number | string):LongReturns the difference of this and the specified Long.Long#toBytes(le?:
boolean):number[]Converts this Long to its byte representation.Long#toBytesLE():
number[]Converts this Long to its little endian byte representation.Long#toBytesBE():
number[]Converts this Long to its big endian byte representation.Long#toInt():
numberConverts the Long to a 32 bit integer, assuming it is a 32 bit integer.Long#toNumber():
numberConverts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).Long#toSigned():
LongConverts this Long to signed.Long#toString(radix?:
number):stringConverts the Long to a string written in the specified radix.Long#toUnsigned():
LongConverts this Long to unsigned.Long#xor(other:
Long | number | string):LongReturns the bitwise XOR of this Long and the given one.
WebAssembly support
WebAssembly supports 64-bit integer arithmetic out of the box, hence a tiny WebAssembly module is used to compute operations like multiplication, division and remainder more efficiently (slow operations like division are around twice as fast), falling back to floating point based computations in JavaScript where WebAssembly is not yet supported, e.g., in older versions of node.
Building
Building the UMD fallback:
$> npm run buildRunning the tests:
$> npm test