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.## Background

As of ECMA-262 5th Edition, "all the positive and negative integers whose magnitude is no greater than 2^{53}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 2

^{53}-1.

Example: 2

^{64}-1 is 1844674407370955

**1615**but in JavaScript it evaluates to 1844674407370955

**2000**.

Furthermore, bitwise operators in JavaScript "deal only with integers in the range −2

^{31}through 2

^{31}−1, inclusive, or in the range 0 through 2

^{32}−1, inclusive. These operators accept any value of the Number type but first convert each such value to one of 2

^{32}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 long`

```
import 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)<br />
`https://cdn.jsdelivr.net/npm/long@VERSION/umd/index.js` (UMD)
```

*From npm via unpkg*:

```
`https://unpkg.com/long@VERSION/index.js` (ESM)<br />
`https://unpkg.com/long@VERSION/umd/index.js` (UMD)
```

Replace `TAG`

respectively `VERSION`

with a specific version or omit it (not recommended in production) to use main/latest.## API

### Constructor

- new
**Long**(low:`number`

, high?:`number`

, unsigned?:`boolean`

)

*signed*integers. See the from

*functions below for more convenient ways of constructing Longs.*

Returns a Long representation of the given string, written using the specified radix.

function for its type.

### Fields

- Long#
**low**:`number`

- Long#
**high**:`number`

- Long#
**unsigned**:`boolean`

### Constants

- Long.
**ZERO**:`Long`

- Long.
**ONE**:`Long`

- Long.
**NEG***ONE***:**`Long`

- Long.
**UZERO**:`Long`

- Long.
**UONE**:`Long`

- Long.
**MAX***VALUE***:**`Long`

- Long.
**MIN***VALUE***:**`Long`

- Long.
**MAX**:*UNSIGNED*VALUE`Long`

### Utility

- Long.
**isLong**(obj:`*`

):`boolean`

- Long.
**fromBits**(lowBits:`number`

, highBits:`number`

, unsigned?:`boolean`

):`Long`

- Long.
**fromBytes**(bytes:`number[]`

, unsigned?:`boolean`

, le?:`boolean`

):`Long`

- Long.
**fromBytesLE**(bytes:`number[]`

, unsigned?:`boolean`

):`Long`

- Long.
**fromBytesBE**(bytes:`number[]`

, unsigned?:`boolean`

):`Long`

- Long.
**fromInt**(value:`number`

, unsigned?:`boolean`

):`Long`

- Long.
**fromNumber**(value:`number`

, unsigned?:`boolean`

):`Long`

- Long.
**fromString**(str:`string`

, unsigned?:`boolean`

, radix?:`number`

)

**fromString**(str:`string`

, radix: `number`

)Returns a Long representation of the given string, written using the specified radix.

- Long.
**fromValue**(val:`*`

, unsigned?:`boolean`

):`Long`

### Methods

- Long#
**add**(addend:`Long | number | string`

):`Long`

- Long#
**and**(other:`Long | number | string`

):`Long`

- Long#
**compare**/**comp**(other:`Long | number | string`

):`number`

`0`

if they are the same, `1`

if the this is greater and `-1`

if the given one is greater.- Long#
**divide**/**div**(divisor:`Long | number | string`

):`Long`

- Long#
**equals**/**eq**(other:`Long | number | string`

):`boolean`

- Long#
**getHighBits**():`number`

- Long#
**getHighBitsUnsigned**():`number`

- Long#
**getLowBits**():`number`

- Long#
**getLowBitsUnsigned**():`number`

- Long#
**getNumBitsAbs**():`number`

- Long#
**greaterThan**/**gt**(other:`Long | number | string`

):`boolean`

- Long#
**greaterThanOrEqual**/**gte**/**ge**(other:`Long | number | string`

):`boolean`

- Long#
**isEven**():`boolean`

- Long#
**isNegative**():`boolean`

- Long#
**isOdd**():`boolean`

- Long#
**isPositive**():`boolean`

- Long#
**isZero**/**eqz**():`boolean`

- Long#
**lessThan**/**lt**(other:`Long | number | string`

):`boolean`

- Long#
**lessThanOrEqual**/**lte**/**le**(other:`Long | number | string`

):`boolean`

- Long#
**modulo**/**mod**/**rem**(divisor:`Long | number | string`

):`Long`

- Long#
**multiply**/**mul**(multiplier:`Long | number | string`

):`Long`

- Long#
**negate**/**neg**():`Long`

- Long#
**not**():`Long`

- Long#
**countLeadingZeros**/**clz**():`number`

- Long#
**countTrailingZeros**/**ctz**():`number`

- Long#
**notEquals**/**neq**/**ne**(other:`Long | number | string`

):`boolean`

- Long#
**or**(other:`Long | number | string`

):`Long`

- Long#
**shiftLeft**/**shl**(numBits:`Long | number | string`

):`Long`

- Long#
**shiftRight**/**shr**(numBits:`Long | number | string`

):`Long`

- Long#
**shiftRightUnsigned**/**shru**/**shr***u***(numBits:**`Long | number | string`

):`Long`

- Long#
**rotateLeft**/**rotl**(numBits:`Long | number | string`

):`Long`

- Long#
**rotateRight**/**rotr**(numBits:`Long | number | string`

):`Long`

- Long#
**subtract**/**sub**(subtrahend:`Long | number | string`

):`Long`

- Long#
**toBytes**(le?:`boolean`

):`number[]`

- Long#
**toBytesLE**():`number[]`

- Long#
**toBytesBE**():`number[]`

- Long#
**toInt**():`number`

- Long#
**toNumber**():`number`

- Long#
**toSigned**():`Long`

- Long#
**toString**(radix?:`number`

):`string`

- Long#
**toUnsigned**():`Long`

- Long#
**xor**(other:`Long | number | string`

):`Long`

## 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 build`

Running the tests:

`$> npm test`