Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do 64bit Integer arithmetic in Node.js?

Anyone have any suggestions on how to perform arithmetic on 64bit integers in Node.js? The node-int64 package doesn't seem to support that.

like image 657
user1502301 Avatar asked Jun 04 '14 12:06

user1502301


People also ask

Does JavaScript support 64-bit int?

Since JavaScript does not natively support 64-bit integers, 64-bit Ids are instead represented as strings. Strings containing 64-bit Ids are distinguished from ordinary strings through use of the Id64String type alias.

What is a 32 bit integer JavaScript?

32-bit integer has range from -2147483648 ( -2^31 ) to 2147483647 ( 2^31 − 1 )

How long is a 64bit integer?

A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). A 64-bit unsigned integer.


2 Answers

Javascript does not support 64 bit integers, because the native number type is a 64-bit double, giving only 53 bits of integer range.

You can create arrays of 32-bit numbers (i.e. Uint32Array) but if there were a 64-bit version of those there'd be no way to copy values from it into standalone variables.

There are some modules around to provide 64bit integer support:

  • node-bigint
  • bignum (based on OpenSSL)
  • int64

Maybe your problem can be solved using one of those libraries.

like image 159
Sarath Avatar answered Oct 10 '22 17:10

Sarath


As of v10.4.0 NodeJS supports the BigInt type natively (see MDN BigInt docs). These support arithmetic operations too.

like image 5
Alex Tullenhoff Avatar answered Oct 10 '22 15:10

Alex Tullenhoff