1

I'm currently learning JavaScript and I'm very surprised there is not built-in way to work with binary numbers. I have to work with integers up to 2^4096 so I just can't use JS integers for my calculus.

My first thought was to use Arrays of 1 and 0. But that's not satisfying since a simple multiplication by 2 requires to shift the whole table.

So how can I work efficiently with binary numbers in JavaScript ?

I'd like not to use any library. I'm interested in how it works, not in using other's abstraction.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
  • possible duplicate of [Is there a bignum library for JavaScript?](http://stackoverflow.com/questions/3072307/is-there-a-bignum-library-for-javascript) – Barmar Oct 12 '13 at 16:41
  • 1
    @T.J.Crowder These are available for integers, but integers can't exceed 2^64 as far as I know – Antoine Pinsard Oct 12 '13 at 16:43
  • @Barmar I'm not looking for a library. – Antoine Pinsard Oct 12 '13 at 16:44
  • 1
    If you want to know how it works, read the source of the library. – Barmar Oct 12 '13 at 16:44
  • @AntoinePinsard: Actually it's `2^32` when you're dealing with the bitwise operators. Sorry, I misread your question. – T.J. Crowder Oct 12 '13 at 16:45
  • I'll bet it uses an array of 32- or 64-bit integers, rather than an array of 1's and 0's. – Barmar Oct 12 '13 at 16:46
  • Maybe [this topic](http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript) with similar issue can help you? – Pumych Oct 12 '13 at 16:49
  • 1
    *"I'm very surprised there is not built-in way to work with binary numbers"* There **is**. There's just no built-in way to work with binary numbers larger than 32-bits, which isn't uncommon (and particularly wasn't uncommon when JavaScript was created). – T.J. Crowder Oct 12 '13 at 16:50
  • How do you mean "work with"? It would improve the question if you listed the kinds of operations do you need to perform. – T.J. Crowder Oct 12 '13 at 16:50
  • @T.J.Crowder Basically additions, multiplications, divisions, modulus, power and comparisons. That's for cryptographic means. I read bits of code from the jsbn library but it's not very well documented. Maybe the Biginteger library would be better to read. – Antoine Pinsard Oct 12 '13 at 16:58
  • @AntoinePinsard: To improve your (very interesting) question, use the "edit" link under the question rather than comments. – T.J. Crowder Oct 12 '13 at 16:59

2 Answers2

1

Javascript doesn't have any biginteger type, so you would need to use an array to hold that much information.

An array of 0 and 1 values would make it easy to implement functions for it, but not very efficient.

A regular number in Javascript is a double precision floating point number, so it can hold 52 bits of numeric information (ref), but you would use slightly less to be far away from any rounding errors, for example 48 bits.

The bitwise operators in Javascript work with 32 bit integers, i.e. a double is converted to a 32 bit integer when used with a bitwise operator. If you want to use the bitwise operators on the data, you could choose to store 32 bits per item in the array.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

JavaScript only supports 53 bit integers.

The best way to store "big integers" is to convert them to strings on server side. And in case you want to manipulate them I would suggest to look at this library https://github.com/rauschma/strint

Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55