1.0.0 • Published 10 years ago

intn v1.0.0

Weekly downloads
9
License
Apache-2.0
Repository
github
Last release
10 years ago

IntN.js - Arbitrary byte size integers in JavaScript

IntN.js is a library for representing and working with arbitrary byte size two's complement integers in JavaScript, both signed and unsigned. Its purpose is to provide a robust and convenient way to work with data types that are not available in JavaScript natively, like 64 bit longs.

Usage

The module exports a function that creates singleton classes representing integers of the specified size in bits (positive multiple of 8). It is compatible with CommonJS and AMD loaders and is exposed globally as dcodeIO.IntN if neither is available.

var IntN  = require("intn");

var Int8  = IntN(8),
    Int16 = IntN(16),
    Int24 = IntN(24),
    Int32 = IntN(32),
    ...
    Int64 = IntN(64),
    ...

Important: The following API documentation references the usage of the classes created by the exported function.

API

Instances are immutable and all methods that return another instance are chainable. Instance values are easily interchangeable using their bytes property or the fromInts and toInts methods.

new IntN(bytes, unsigned=)

Constructs a new IntN, where N is the number of bits represented by this class.

ParameterTypeDescription
bytes!Array.<number> | numberByte values, least significant first
unsignedbooleanWhether unsigned or signed, defaults to false for signed

IntN.BITS

Number of bits represented by this IntN class.

@typenumber
@accesspublic const

IntN.BYTES

Number of bytes represented by this IntN class.

@typenumber
@accesspublic const

IntN.MAX_UNSIGNED_VALUE

Maximum unsigned value.

@type!IntN
@accesspublic const

IntN.MAX_VALUE

Maximum signed value.

@type!IntN
@accesspublic const

IntN.MIN_VALUE

Minimum signed value.

@type!IntN
@accesspublic const

IntN.NEG_ONE

Negative signed one.

@type!IntN
@accesspublic const

IntN.ONE

Signed one.

@type!IntN
@accesspublic const

IntN.UONE

Unsigned one.

@type!IntN
@accesspublic const

IntN.UZERO

Unsigned zero.

@type!IntN
@accesspublic const

IntN.ZERO

Signed zero.

@type!IntN
@accesspublic const

IntN.add(augend, addend)

Adds the specified IntNs. Does not type check arguments.

ParameterTypeDescription
augend!IntNAugend
addend!IntNAddend
@returns!IntNSum

IntN.divide(dividend, divisor)

Divides the specified dividend by the specified divisor and returns both the quotient and the remainder. Does not type check arguments.

ParameterTypeDescription
dividend!IntNDividend
divisor!IntNDivisor
@returns!{quotient: !IntN, remainder: !IntN}Quotient and remainder

IntN.fromInt(value, unsigned=)

Constructs an IntN from a 32 bit integer value.

ParameterTypeDescription
valuenumberInteger value
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!IntN

IntN.fromInts(ints, unsigned=)

Reassembles an IntN from an array of 32 bit integers, least significant first.

ParameterTypeDescription
ints!Array.<number>Array of 32 bit integers
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!IntN

IntN.fromNumber(value, unsigned=)

Constructs an IntN from a number (double, 52 bit mantissa) value. This differs from IntN.fromInt in using arithmetic operations on numbers instead of logical operations on 32 bit integers, which works reliably up to a maximum positive or negative value of 2^53-1.

ParameterTypeDescription
valuenumberNumber value
unsignedbooleanWhether unsigned or not, defaults to false for signed
@returns!IntN
@seehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER
@seehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

IntN.fromString(value, unsigned=, radix=)

Converts a string using the specified radix to an IntN.

ParameterTypeDescription
valuestringString
unsignedboolean | numberWhether unsigned or not, defaults to false for signed (omittable)
radixnumberRadix (2-36), defaults to 10
@returns!IntN
@throwsRangeErrorIf radix is out of range
@throwsErrorIf value contains illegal characters

IntN.isIntN(obj)

Tests if an object is an N bit integer, where N is this class's number of bits.

ParameterTypeDescription
obj***Object to test
@returnsbooleantrue if it is an N bit integer, otherwise false

IntN.multiply(multiplicand, multiplier)

Multiplies the specified IntNs and returns the product. Does not type check arguments.

ParameterTypeDescription
multiplicand!IntNMultiplicand
multiplier!IntNMultiplier
@returns!IntNProduct

IntN.subtract(minuend, subtrahend)

Subtracts the second from the first specified IntN. Does not type check arguments.

ParameterTypeDescription
minuend!IntNMinuend
subtrahend!IntNSubtrahend
@returns!IntNDifference

IntN.valueOf(val)

Converts the specified value to an IntN.

ParameterTypeDescription
val!IntN | number | string | !{bytes: !Array.<number>, unsigned: boolean} | {low: number, high: number, unsigned: boolean}Value
@returns!IntN

IntN#bytes

Represented byte values, least significant first.

@type!Array.<number>

IntN#unsigned

Whether unsigned or otherwise signed.

@typeboolean

IntN#absolute()

Returns this IntN's absolute value as an unsigned IntN.

ParameterTypeDescription
@returns!IntNAbsolute

IntN#add(addend)

Adds the specified to this IntN.

ParameterTypeDescription
addend!IntN | number | stringAddend
@returns!IntNSum

IntN#and(other)

Performs a bitwise and (&) operation and returns the resulting Int.

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

IntN#cast(TargetIntN, unsigned=)

Casts this IntN of size N to the specified target IntN of size M.

ParameterTypeDescription
TargetIntN!FunctionTarget IntN class
unsignedbooleanWhether unsigned or not, defaults to this' IntN#unsigned
@returns!IntN

IntN#compare(other)

Compares this IntN with the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsnumber0 if both are the same, 1 if this is greater and -1 if the specified is greater

IntN#divide(divisor)

Divides this IntN by the specified and returns the quotient.

ParameterTypeDescription
divisor!IntN | number | stringDivisor
@returns!IntNQuotient

IntN#equals(other)

Tests if this IntN equals the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#greaterThan(other)

Tests if this IntN is greater than (>) the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#greaterThanEqual(other)

Tests if this IntN is greater than or equal (>=) the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#isEven()

Tests if this IntN is even.

ParameterTypeDescription
@returnsboolean

IntN#isNegative()

Tests if this IntN is (signed and) negative.

ParameterTypeDescription
@returnsboolean

IntN#isOdd()

Tests if this IntN is odd.

ParameterTypeDescription
@returnsboolean

IntN#isPositive()

Tests if this IntN is (unsigned or) positive.

ParameterTypeDescription
@returnsboolean

IntN#isSet(i)

Evaluates the bit at the specified position. Using this method is usually much faster than alternative ways.

ParameterTypeDescription
inumberPosition (0 to (N-1))
@returnsbooleantrue if the bit is 1, false if it is 0

IntN#isSigned()

Tests if this IntN is signed.

ParameterTypeDescription
@returnsboolean

IntN#isUnsigned()

Tests if this IntN is unsigned.

ParameterTypeDescription
@returnsboolean

IntN#isZero()

Tests if this IntN is zero.

ParameterTypeDescription
@returnsboolean

IntN#lessThan(other)

Tests if this IntN is less than (<) the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#lessThanEqual(other)

Tests if this IntN is less than or equal (<=) the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#modulo(divisor)

Divides this IntN by the specified and returns the remainder.

ParameterTypeDescription
divisor!IntN | number | stringDivisor
@returns!IntNRemainder

IntN#multiply(multiplier)

Multiplies this IntN with the specified and returns the product.

ParameterTypeDescription
multiplier!IntN | number | stringMultiplier
@returns!IntNProduct

IntN#negate()

Negates this IntN (*-1).

ParameterTypeDescription
@returns!IntNNegation

IntN#not()

Performs a bitwise not (~) operation and returns the result.

ParameterTypeDescription
@returns!IntN

IntN#notEquals(other)

Tests if this IntN does not equal the specified.

ParameterTypeDescription
other!IntN | number | stringOther value
@returnsboolean

IntN#or(other)

Performs a bitwise or (|) operation and returns the resulting Int.

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

IntN#set(i, isSet)

Sets the bit at the specified position and returns the result. Using this method is usually much faster than alternative ways.

ParameterTypeDescription
inumberPosition (0 to (N-1))
isSetbooleantrue to set the bit to 1, false to set it to 0
@returns!IntN

IntN#shiftLeft(numBits)

Performs a shift left (<<) operation and returns the result.

ParameterTypeDescription
numBits!IntN | numberNumber of bits
@returns!IntN

IntN#shiftRight(numBits, logical=)

Performs a shift right (>>, >>>) operation and returns the result.

ParameterTypeDescription
numBits!IntN | numberNumber of bits
logicalbooleanWhether to perform a logical (>>>) shift right, defaults to false for an arithmetic shift right (>>)
@returns!IntN

IntN#shiftRightUnsigned(numBits)

Performs an unsigned shift right (>>>) operation and returns the result.

ParameterTypeDescription
numBits!IntN | numberNumber of bits
@returns!IntNShifted

IntN#size()

Returns the number of bits required to fully represent this IntN's value.

ParameterTypeDescription
@returnsnumberShift of the most significant bit (0 to N)

IntN#subtract(subtrahend)

Subtracts the specified from this IntN and returns the difference.

ParameterTypeDescription
subtrahend!IntN | number | stringSubtrahend
@returns!IntNDifference

IntN#toDebug(addSpaces=)

Converts this IntN to its full binary representation. This returns N (number of bits) binary digits for testing and debugging, followed by the character U if unsigned.

ParameterTypeDescription
addSpacesbooleanWhether to insert spaces between bytes, defaults to false
@returnsstring

IntN#toInt(unsigned=)

Converts this IntN to a 32 bit integer.

ParameterTypeDescription
unsignedbooleanWhether unsigned or not, defaults to this' IntN#unsigned
@returnsnumber

IntN#toInts()

Disassembles this IntN into an array of 32 bit integers, least significant first.

ParameterTypeDescription
@returns!Array.<number>

IntN#toNumber()

Converts this IntN to a number (double, 52 bit mantissa) value. This differs from IntN#toInt in using arithmetic operations on numbers instead of logical operations on 32 bit integers, which works reliably up to a maximum positive or negative value of 2^53-1. A maximum of 56 bits is evaluated.

ParameterTypeDescription
@returnsnumber
@seehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER
@seehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

IntN#toSigned()

Converts this IntN to signed and returns the result.

ParameterTypeDescription
@returns!IntN

IntN#toString(radix)

Converts this IntN to a string of the specified radix.

ParameterTypeDescription
radix!IntN | number | stringRadix (2-36), defaults to 10
@returnsstring
@throwsRangeErrorIf radix is out of range

IntN#toUnsigned()

Converts this IntN to unsigned and returns the result.

ParameterTypeDescription
@returns!IntN

IntN#xor(other)

Performs a bitwise xor (^) operation and returns the result.

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

Aliases

Most of the methods have a couple of aliases to maintain compatibility with other libraries, to make it more convenient to use or simply to keep your code small:

General utility:
  • isIntN: isIntNBITS with NBITS being the number of bits provided to IntN (e.g. 32)
Arithmetic evaluation:
  • compare: comp
  • equals: eq, equal, ==
  • notEquals: ne, notEqual, !=
  • lessThan: lt, less, <
  • lessThanEqual: lte, lessThanOrEqual, <=
  • greaterThan: gt, greater, >
  • greaterThanEqual: gte, greaterThanOrEqual, >=
Bitwise operations:
  • not: ~
  • and: &
  • or: |
  • xor: ^
  • shiftLeft: lsh, leftShift, <<
  • shiftRight: rsh, rightShift, >>
  • shiftRightUnsigned: rshu, rightShiftUnsigned, >>>
Arithmetic operations:
  • add: plus, +
  • negate: neg, !
  • subtract: sub, minus, -
  • absolute: abs, ||
  • multiply: mult, *
  • divide: div, /
  • modulo: mod, %

If you like it rather formal:

var a = Int32.fromNumber(1),
    b = Int32.fromNumber(2);
    
var c = a['+'](b)['/'](3);

Downloads

License: Apache License, Version 2.0

1.0.0

10 years ago

0.14.0

10 years ago

0.13.0

10 years ago

0.12.0

10 years ago

0.11.0

10 years ago

0.10.0

10 years ago

0.9.2

10 years ago

0.9.1

10 years ago

0.9.0

10 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago