3.2.7 • Published 8 years ago

long-mutable v3.2.7

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
8 years ago

long.js - A Long class for representing a 64 bit two's-complement integer

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.

Build Status Donate

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 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());
...

API

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.

ParameterTypeDescription
lownumberThe low (signed) 32 bits of the long
highnumberThe high (signed) 32 bits of the long
unsignedbooleanWhether unsigned or not, defaults to false for signed

Long.MAX_UNSIGNED_VALUE

Maximum unsigned value.

@type!Long

Long.MAX_VALUE

Maximum signed value.

@type!Long

Long.MIN_VALUE

Minimum signed value.

@type!Long

Long.NEG_ONE

Signed negative one.

@type!Long

Long.ONE

Signed one.

@type!Long

Long.UONE

Unsigned one.

@type!Long

Long.UZERO

Unsigned zero.

@type!Long

Long.ZERO

Signed zero.

@type!Long

Long.fromBits(lowBits, highBits, unsigned=)

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.

ParameterTypeDescription
lowBitsnumberThe low 32 bits
highBitsnumberThe high 32 bits
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!LongThe corresponding Long value

Long.fromInt(value, unsigned=)

Returns a Long representing the given 32 bit integer value.

ParameterTypeDescription
valuenumberThe 32 bit integer in question
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!LongThe corresponding Long value

Long.fromNumber(value, unsigned=)

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

ParameterTypeDescription
valuenumberThe number in question
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!LongThe corresponding Long value

Long.fromString(str, unsigned=, radix=)

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

ParameterTypeDescription
strstringThe textual representation of the Long
unsignedboolean | numberWhether unsigned or not, defaults to false for signed
radixnumberThe radix in which the text is written (2-36), defaults to 10
@returns!LongThe corresponding Long value

Long.isLong(obj)

Tests if the specified object is a Long.

ParameterTypeDescription
obj***Object
@returnsboolean

Long.fromValue(val)

Converts the specified value to a Long.

ParameterTypeDescription
val!Long | number | string | !{low: number, high: number, unsigned: boolean}Value
@returns!Long

Long#high

The high 32 bits as a signed value.

@typenumber

Long#low

The low 32 bits as a signed value.

@typenumber

Long#unsigned

Whether unsigned or not.

@typeboolean

Long#add(addend)

Returns the sum of this and the specified Long.

ParameterTypeDescription
addend!Long | number | stringAddend
@returns!LongSum

Long#and(other)

Returns the bitwise AND of this Long and the specified.

ParameterTypeDescription
other!Long | number | stringOther Long
@returns!Long

Long#compare/comp(other)

Compares this Long's value with the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsnumber0 if they are the same, 1 if the this is greater and -1 if the given one is greater

Long#divide/div(divisor)

Returns this Long divided by the specified.

ParameterTypeDescription
divisor!Long | number | stringDivisor
@returns!LongQuotient

Long#equals/eq(other)

Tests if this Long's value equals the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#getHighBits()

Gets the high 32 bits as a signed integer.

ParameterTypeDescription
@returnsnumberSigned high bits

Long#getHighBitsUnsigned()

Gets the high 32 bits as an unsigned integer.

ParameterTypeDescription
@returnsnumberUnsigned high bits

Long#getLowBits()

Gets the low 32 bits as a signed integer.

ParameterTypeDescription
@returnsnumberSigned low bits

Long#getLowBitsUnsigned()

Gets the low 32 bits as an unsigned integer.

ParameterTypeDescription
@returnsnumberUnsigned low bits

Long#getNumBitsAbs()

Gets the number of bits needed to represent the absolute value of this Long.

ParameterTypeDescription
@returnsnumber

Long#greaterThan/gt(other)

Tests if this Long's value is greater than the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#greaterThanOrEqual/gte(other)

Tests if this Long's value is greater than or equal the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#isEven()

Tests if this Long's value is even.

ParameterTypeDescription
@returnsboolean

Long#isNegative()

Tests if this Long's value is negative.

ParameterTypeDescription
@returnsboolean

Long#isOdd()

Tests if this Long's value is odd.

ParameterTypeDescription
@returnsboolean

Long#isPositive()

Tests if this Long's value is positive.

ParameterTypeDescription
@returnsboolean

Long#isZero()

Tests if this Long's value equals zero.

ParameterTypeDescription
@returnsboolean

Long#lessThan/lt(other)

Tests if this Long's value is less than the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#lessThanOrEqual/lte(other)

Tests if this Long's value is less than or equal the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#modulo/mod(divisor)

Returns this Long modulo the specified.

ParameterTypeDescription
divisor!Long | number | stringDivisor
@returns!LongRemainder

Long#multiply/mul(multiplier)

Returns the product of this and the specified Long.

ParameterTypeDescription
multiplier!Long | number | stringMultiplier
@returns!LongProduct

Long#negate/neg()

Negates this Long's value.

ParameterTypeDescription
@returns!LongNegated Long

Long#not()

Returns the bitwise NOT of this Long.

ParameterTypeDescription
@returns!Long

Long#notEquals/neq(other)

Tests if this Long's value differs from the specified's.

ParameterTypeDescription
other!Long | number | stringOther value
@returnsboolean

Long#or(other)

Returns the bitwise OR of this Long and the specified.

ParameterTypeDescription
other!Long | number | stringOther Long
@returns!Long

Long#shiftLeft/shl(numBits)

Returns this Long with bits shifted to the left by the given amount.

ParameterTypeDescription
numBitsnumber | !LongNumber of bits
@returns!LongShifted Long

Long#shiftRight/shr(numBits)

Returns this Long with bits arithmetically shifted to the right by the given amount.

ParameterTypeDescription
numBitsnumber | !LongNumber of bits
@returns!LongShifted Long

Long#shiftRightUnsigned/shru(numBits)

Returns this Long with bits logically shifted to the right by the given amount.

ParameterTypeDescription
numBitsnumber | !LongNumber of bits
@returns!LongShifted Long

Long#subtract/sub(subtrahend)

Returns the difference of this and the specified Long.

ParameterTypeDescription
subtrahend!Long | number | stringSubtrahend
@returns!LongDifference

Long#toInt()

Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.

ParameterTypeDescription
@returnsnumber

Long#toNumber()

Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).

ParameterTypeDescription
@returnsnumber

Long#toSigned()

Converts this Long to signed.

ParameterTypeDescription
@returns!LongSigned long

Long#toString(radix=)

Converts the Long to a string written in the specified radix.

ParameterTypeDescription
radixnumberRadix (2-36), defaults to 10
@returnsstring
@throwsRangeErrorIf radix is out of range

Long#toUnsigned()

Converts this Long to unsigned.

ParameterTypeDescription
@returns!LongUnsigned long

Long#xor(other)

Returns the bitwise XOR of this Long and the given one.

ParameterTypeDescription
other!Long | number | stringOther Long
@returns!Long

Downloads

License

Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html

3.2.7

8 years ago

3.2.6

8 years ago

3.2.5

8 years ago

3.2.4

8 years ago

3.2.3

8 years ago

3.2.2

8 years ago

3.2.1

8 years ago

3.2.0

8 years ago