Socket
Socket
Sign inDemoInstall

long

Package Overview
Dependencies
0
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    long

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


Version published
Weekly downloads
20M
decreased by-8.18%
Maintainers
1
Install size
190 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

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

Keywords

FAQs

Last updated on 01 Apr 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc