What is long?
The 'long' npm package provides a comprehensive library for representing, converting, and performing arithmetic operations on 64-bit integers. This is particularly useful in environments like JavaScript, where the standard Number type can only safely represent integers up to 53 bits.
What are long's main functionalities?
Creation of Long integers
This feature allows the creation of 64-bit integers. The constructor takes two arguments representing the low and high 32 bits of the integer, respectively.
const Long = require('long');
let longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
Arithmetic operations
This feature supports basic arithmetic operations such as addition, subtraction, multiplication, and division on 64-bit integers.
const Long = require('long');
let long1 = new Long(1, 0);
let long2 = new Long(1, 0);
let sum = long1.add(long2);
Comparison operations
This feature enables comparison operations like less than, greater than, equals, etc., between two Long instances.
const Long = require('long');
let long1 = new Long(1, 0);
let long2 = new Long(2, 0);
let isLessThan = long1.lessThan(long2);
Other packages similar to long
bignum
The 'bignum' package also provides arbitrary precision arithmetic operations. Unlike 'long', which is limited to 64-bit, 'bignum' can handle much larger numbers, making it suitable for applications requiring high precision.
bigint
Similar to 'long', 'bigint' supports large integer operations. However, 'bigint' is built into modern JavaScript environments as a native type, offering better performance and integration with the language compared to 'long', which is a library.
Long.js
A Long class for representing a 64-bit two's-complement integer value derived from the Closure Library
for stand-alone use.
Why?
As of the ECMAScript specification, number types have a maximum value
of 2^53. Beyond that, JavaScript falls back to double internally. However, in some use cases it is required to be able
to perform reliable mathematical and/or bitwise operations on the full 64bits. This is where Long.js comes into play.
Features
- CommonJS compatible
- RequireJS/AMD compatible
- Shim compatible (include the script, then use var ByteBuffer = dcodeIO.ByteBuffer;)
- node.js compatible, also available via npm
- Fully documented using jsdoc3
- Zero production dependencies
- Small footprint
Long
- Construction from high and low bits as 32bit integers:
new Long(low, high)
and Long.fromBits(low, high)
- ...from a 32bit integer:
Long.fromInt(value)
including a cache for frequently used small numbers - ...from a number which may internally be a number or double type:
Long.fromNumber(value)
- ...from a string:
Long.fromString(value[, radix=10])
- Conversion to a 32bit integer:
Long#toInt()
- ...to a number:
Long#toNumber()
- ...to a string:
Long#toString([radix=10])
- Getters for high and low bits as 32bit integers:
Long#getLowBits()
, Long#getHighBits()
, Long#getLowBitsUnsigned()
- Comparison:
Long#equals(other)
, Long#notEquals(other)
, Long#lessThan(other)
, Long#lessThanOrEqual(other)
,
Long#greaterThan(other)
, Long#greaterThanOrEqual(other)
, Long#compare(other)
- Common tests:
Long#isZero()
, Long#isNegative()
, Long#isOdd()
, Long#isEven()
- Math:
Long#negate()
, Long#add(other)
, Long#subtract(other)
, Long#multiply(other)
, Long#div(other)
,
Long#modulo(other)
- Bitwise operations:
Long#not()
, Long#and(other)
, Long#or(other)
, Long#xor(other)
, Long#shiftLeft(numBits)
,
Long#shiftRight(numBits)
, Long#shiftRightUnsigned(numBits)
Usage
node.js / CommonJS
Install: npm install long
var Long = require("Long");
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
...
RequireJS / AMD
require(["path/to/Long.js"], function(Long) {
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
});
Browser / shim
<script src="//raw.github.com/dcodeIO/Long.js/master/Long.min.js"></script>
var Long = dcodeIO.Long;
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
alert(longVal.toString());
Documentation
Downloads
License
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html