Class: Long

ByteBuffer. Long

new Long(low, high)

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 MINVALUE (-2^63) because -MINVALUE == 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 Description
low number

The low (signed) 32 bits of the long.

high number

The high (signed) 32 bits of the long.

Members

<static> MAX_VALUE :Long

<static> MIN_VALUE :Long

<static> NEG_ONE :Long

<static> ONE :Long

<static> ZERO :Long

Methods

<static> fromBits(lowBits, highBits) → {Long}

Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits.

Parameters:
Name Type Description
lowBits number

The low 32-bits.

highBits number

The high 32-bits.

Returns:

The corresponding Long value.

Type
Long

<static> fromInt(value) → {Long}

Returns a Long representing the given (32-bit) integer value.

Parameters:
Name Type Description
value number

The 32-bit integer in question.

Returns:

The corresponding Long value.

Type
Long

<static> fromNumber(value) → {Long}

Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.

Parameters:
Name Type Description
value number

The number in question.

Returns:

The corresponding Long value.

Type
Long

<static> fromString(str, opt_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.

opt_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

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

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

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

toString(opt_radix) → {string}

Parameters:
Name Type Argument Description
opt_radix number <optional>

The radix in which the text should be written.

Returns:

The textual representation of this value.

Type
string

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