new Long(low, high, unsigned)
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.
The internal representation of a long is the two given signed, 32-bit values.
We use 32-bit pieces because these are the size of integers on which
Javascript performs bit-operations. For operations like addition and
multiplication, we split each number into 16-bit pieces, which can easily be
multiplied within Javascript's floating-point representation without overflow
or change in sign.
In the algorithms below, we frequently reduce the negative case to the
positive case by negating the input(s) and then post-processing the result.
Note that we must ALWAYS check specially whether those values are MIN_VALUE
(-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
a positive number, it overflows back into a negative). Not handling this
case would often result in infinite recursion.
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
low |
number | The low (signed) 32 bits of the long. | |
high |
number | The high (signed) 32 bits of the long. | |
unsigned |
boolean |
<optional> |
Whether unsigned or not. Defaults to `false` (signed). |
Members
-
<static> MAX_SIGNED_VALUE :Long
-
-
<static> MAX_UNSIGNED_VALUE :Long
-
-
<static> MAX_VALUE :Long
-
Alias of Long.MAX_SIGNED_VALUE for goog.math.Long compatibility.
-
<static> MIN_SIGNED_VALUE :Long
-
-
<static> MIN_UNSIGNED_VALUE :Long
-
-
<static> MIN_VALUE :Long
-
Alias of Long.MIN_SIGNED_VALUE for goog.math.Long compatibility.
-
<static> NEG_ONE :Long
-
-
<static> ONE :Long
-
-
<static> ZERO :Long
-
-
high :number
-
The high 32 bits as a signed value.
-
low :number
-
The low 32 bits as a signed value.
-
unsigned :boolean
-
Whether unsigned or not.
Methods
-
<static> from28Bits(part0, part1, part2, unsigned) → {Long}
-
Returns a Long representing the 64bit integer that comes by concatenating the given low, middle and high bits. Each is assumed to use 28 bits.
Parameters:
Name Type Argument Description part0
number The low 28 bits part1
number The middle 28 bits part2
number The high 28 (8) bits unsigned
boolean <optional>
Whether unsigned or not. Defaults to false (signed). Returns:
- Type
- Long
-
<static> fromBits(lowBits, highBits, unsigned) → {Long}
-
Returns a Long representing the 64bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.
Parameters:
Name Type Argument Description lowBits
number The low 32 bits. highBits
number The high 32 bits. unsigned
boolean <optional>
Whether unsigned or not. Defaults to false (signed). Returns:
The corresponding Long value.- Type
- Long
-
<static> fromInt(value, unsigned) → {Long}
-
Returns a Long representing the given (32-bit) integer value.
Parameters:
Name Type Argument Description value
number The 32-bit integer in question. unsigned
boolean <optional>
Whether unsigned or not. Defaults to false (signed). Returns:
The corresponding Long value.- Type
- Long
-
<static> fromNumber(value, unsigned) → {Long}
-
Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
Parameters:
Name Type Argument Description value
number The number in question. unsigned
boolean <optional>
Whether unsigned or not. Defaults to false (signed). Returns:
The corresponding Long value.- Type
- Long
-
<static> fromString(str, unsigned, radix) → {Long}
-
Returns a Long representation of the given string, written using the given radix.
Parameters:
Name Type Argument Description str
string The textual representation of the Long. unsigned
boolean | number <optional>
Whether unsigned or not. Defaults to false (signed). radix
number <optional>
The radix in which the text is written. Returns:
The corresponding Long value.- Type
- Long
-
add(other) → {Long}
-
Returns the sum of this and the given Long.
Parameters:
Name Type Description other
Long Long to add to this one. Returns:
The sum of this and the given Long.- Type
- Long
-
and(other) → {Long}
-
Returns the bitwise-AND of this Long and the given one.
Parameters:
Name Type Description other
Long The Long with which to AND. Returns:
The bitwise-AND of this and the other.- Type
- Long
-
clone() → {Long}
-
Returns:
Cloned instance with the same low/high bits and unsigned flag.- Type
- Long
-
compare(other) → {number}
-
Compares this Long with the given one.
Parameters:
Name Type Description other
Long Long to compare against. Returns:
0 if they are the same, 1 if the this is greater, and -1 if the given one is greater.- Type
- number
-
div(other) → {Long}
-
Returns this Long divided by the given one.
Parameters:
Name Type Description other
Long Long by which to divide. Returns:
This Long divided by the given one.- Type
- Long
-
equals(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long equals the other.- Type
- boolean
-
getHighBits() → {number}
-
Returns:
The high 32 bits as a signed value.- Type
- number
-
getHighBitsUnsigned() → {number}
-
Returns:
The high 32 bits as an unsigned value.- Type
- number
-
getLowBits() → {number}
-
Returns:
The low 32 bits as a signed value.- Type
- number
-
getLowBitsUnsigned() → {number}
-
Returns:
The low 32 bits as an unsigned value.- Type
- number
-
getNumBitsAbs() → {number}
-
Returns:
Returns the number of bits needed to represent the absolute value of this Long.- Type
- number
-
greaterThan(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long is greater than the other.- Type
- boolean
-
greaterThanOrEqual(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long is greater than or equal to the other.- Type
- boolean
-
isEven() → {boolean}
-
Returns:
Whether this value is even.- Type
- boolean
-
isNegative() → {boolean}
-
Returns:
Whether this value is negative.- Type
- boolean
-
isOdd() → {boolean}
-
Returns:
Whether this value is odd.- Type
- boolean
-
isZero() → {boolean}
-
Returns:
Whether this value is zero.- Type
- boolean
-
lessThan(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long is less than the other.- Type
- boolean
-
lessThanOrEqual(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long is less than or equal to the other.- Type
- boolean
-
modulo(other) → {Long}
-
Returns this Long modulo the given one.
Parameters:
Name Type Description other
Long Long by which to mod. Returns:
This Long modulo the given one.- Type
- Long
-
multiply(other) → {Long}
-
Returns the product of this and the given long.
Parameters:
Name Type Description other
Long Long to multiply with this. Returns:
The product of this and the other.- Type
- Long
-
negate() → {Long}
-
Returns:
The negation of this value.- Type
- Long
-
not() → {Long}
-
Returns:
The bitwise-NOT of this value.- Type
- Long
-
notEquals(other) → {boolean}
-
Parameters:
Name Type Description other
Long Long to compare against. Returns:
Whether this Long does not equal the other.- Type
- boolean
-
or(other) → {Long}
-
Returns the bitwise-OR of this Long and the given one.
Parameters:
Name Type Description other
Long The Long with which to OR. Returns:
The bitwise-OR of this and the other.- Type
- Long
-
shiftLeft(numBits) → {Long}
-
Returns this Long with bits shifted to the left by the given amount.
Parameters:
Name Type Description numBits
number The number of bits by which to shift. Returns:
This shifted to the left by the given amount.- Type
- Long
-
shiftRight(numBits) → {Long}
-
Returns this Long with bits shifted to the right by the given amount.
Parameters:
Name Type Description numBits
number The number of bits by which to shift. Returns:
This shifted to the right by the given amount.- Type
- Long
-
shiftRightUnsigned(numBits) → {Long}
-
Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit.
Parameters:
Name Type Description numBits
number The number of bits by which to shift. Returns:
This shifted to the right by the given amount, with zeros placed into the new leading bits.- Type
- Long
-
subtract(other) → {Long}
-
Returns the difference of this and the given Long.
Parameters:
Name Type Description other
Long Long to subtract from this. Returns:
The difference of this and the given Long.- Type
- Long
-
toInt() → {number}
-
Returns:
The value, assuming it is a 32-bit integer.- Type
- number
-
toNumber() → {number}
-
Returns:
The closest floating-point representation to this value.- Type
- number
-
toSigned() → {Long}
-
Returns:
Signed long- Type
- Long
-
toString(radix) → {string}
-
Parameters:
Name Type Argument Description radix
number <optional>
The radix in which the text should be written. Returns:
The textual representation of this value.- Type
- string
-
toUnsigned() → {Long}
-
Returns:
Unsigned long- Type
- Long
-
xor(other) → {Long}
-
Returns the bitwise-XOR of this Long and the given one.
Parameters:
Name Type Description other
Long The Long with which to XOR. Returns:
The bitwise-XOR of this and the other.- Type
- Long