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.
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.
The class is compatible with CommonJS and AMD loaders and is exposed globally as dcodeIO.Long
if neither is available.
var Long = require("long");
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
...
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.
Parameter | Type | Description |
---|---|---|
low | number | The low (signed) 32 bits of the long |
high | number | The high (signed) 32 bits of the long |
unsigned | boolean | Whether unsigned or not, defaults to false for signed |
Maximum unsigned value.
@type | !Long |
Maximum signed value.
@type | !Long |
Minimum signed value.
@type | !Long |
Signed negative one.
@type | !Long |
Signed one.
@type | !Long |
Unsigned one.
@type | !Long |
Unsigned zero.
@type | !Long |
Signed zero.
@type | !Long |
Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.
Parameter | Type | Description |
---|---|---|
lowBits | number | The low 32 bits |
highBits | number | The high 32 bits |
unsigned | boolean | Whether unsigned or not, defaults to false for signed |
@returns | !Long | The corresponding Long value |
Returns a Long representing the given 32 bit integer value.
Parameter | Type | Description |
---|---|---|
value | number | The 32 bit integer in question |
unsigned | boolean | Whether unsigned or not, defaults to false for signed |
@returns | !Long | The corresponding Long value |
Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
Parameter | Type | Description |
---|---|---|
value | number | The number in question |
unsigned | boolean | Whether unsigned or not, defaults to false for signed |
@returns | !Long | The corresponding Long value |
Returns a Long representation of the given string, written using the specified radix.
Parameter | Type | Description |
---|---|---|
str | string | The textual representation of the Long |
unsigned | boolean | number | Whether unsigned or not, defaults to false for signed |
radix | number | The radix in which the text is written (2-36), defaults to 10 |
@returns | !Long | The corresponding Long value |
Tests if the specified object is a Long.
Parameter | Type | Description |
---|---|---|
obj | *** | Object |
@returns | boolean |
Converts the specified value to a Long.
Parameter | Type | Description |
---|---|---|
val | !Long | number | string | !{low: number, high: number, unsigned: boolean} | Value |
@returns | !Long |
The high 32 bits as a signed value.
@type | number |
The low 32 bits as a signed value.
@type | number |
Whether unsigned or not.
@type | boolean |
Returns the sum of this and the specified Long.
Parameter | Type | Description |
---|---|---|
addend | !Long | number | string | Addend |
@returns | !Long | Sum |
Returns the bitwise AND of this Long and the specified.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other Long |
@returns | !Long |
Compares this Long's value with the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | number | 0 if they are the same, 1 if the this is greater and -1 if the given one is greater |
Returns this Long divided by the specified.
Parameter | Type | Description |
---|---|---|
divisor | !Long | number | string | Divisor |
@returns | !Long | Quotient |
Tests if this Long's value equals the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Gets the high 32 bits as a signed integer.
Parameter | Type | Description |
---|---|---|
@returns | number | Signed high bits |
Gets the high 32 bits as an unsigned integer.
Parameter | Type | Description |
---|---|---|
@returns | number | Unsigned high bits |
Gets the low 32 bits as a signed integer.
Parameter | Type | Description |
---|---|---|
@returns | number | Signed low bits |
Gets the low 32 bits as an unsigned integer.
Parameter | Type | Description |
---|---|---|
@returns | number | Unsigned low bits |
Gets the number of bits needed to represent the absolute value of this Long.
Parameter | Type | Description |
---|---|---|
@returns | number |
Tests if this Long's value is greater than the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Tests if this Long's value is greater than or equal the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Tests if this Long's value is even.
Parameter | Type | Description |
---|---|---|
@returns | boolean |
Tests if this Long's value is negative.
Parameter | Type | Description |
---|---|---|
@returns | boolean |
Tests if this Long's value is odd.
Parameter | Type | Description |
---|---|---|
@returns | boolean |
Tests if this Long's value is positive.
Parameter | Type | Description |
---|---|---|
@returns | boolean |
Tests if this Long's value equals zero.
Parameter | Type | Description |
---|---|---|
@returns | boolean |
Tests if this Long's value is less than the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Tests if this Long's value is less than or equal the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Returns this Long modulo the specified.
Parameter | Type | Description |
---|---|---|
divisor | !Long | number | string | Divisor |
@returns | !Long | Remainder |
Returns the product of this and the specified Long.
Parameter | Type | Description |
---|---|---|
multiplier | !Long | number | string | Multiplier |
@returns | !Long | Product |
Negates this Long's value.
Parameter | Type | Description |
---|---|---|
@returns | !Long | Negated Long |
Returns the bitwise NOT of this Long.
Parameter | Type | Description |
---|---|---|
@returns | !Long |
Tests if this Long's value differs from the specified's.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other value |
@returns | boolean |
Returns the bitwise OR of this Long and the specified.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other Long |
@returns | !Long |
Returns this Long with bits shifted to the left by the given amount.
Parameter | Type | Description |
---|---|---|
numBits | number | !Long | Number of bits |
@returns | !Long | Shifted Long |
Returns this Long with bits arithmetically shifted to the right by the given amount.
Parameter | Type | Description |
---|---|---|
numBits | number | !Long | Number of bits |
@returns | !Long | Shifted Long |
Returns this Long with bits logically shifted to the right by the given amount.
Parameter | Type | Description |
---|---|---|
numBits | number | !Long | Number of bits |
@returns | !Long | Shifted Long |
Returns the difference of this and the specified Long.
Parameter | Type | Description |
---|---|---|
subtrahend | !Long | number | string | Subtrahend |
@returns | !Long | Difference |
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
Parameter | Type | Description |
---|---|---|
@returns | number |
Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
Parameter | Type | Description |
---|---|---|
@returns | number |
Converts this Long to signed.
Parameter | Type | Description |
---|---|---|
@returns | !Long | Signed long |
Converts the Long to a string written in the specified radix.
Parameter | Type | Description |
---|---|---|
radix | number | Radix (2-36), defaults to 10 |
@returns | string | |
@throws | RangeError | If radix is out of range |
Converts this Long to unsigned.
Parameter | Type | Description |
---|---|---|
@returns | !Long | Unsigned long |
Returns the bitwise XOR of this Long and the given one.
Parameter | Type | Description |
---|---|---|
other | !Long | number | string | Other Long |
@returns | !Long |
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html