I collected funcs searching for conversion from integer to binary with different sizes, and re-visiting the code I'm thinking maybe what I recorded as 64 bit integer was actually signed 32 bit?

```
const readInt64 = (b) => {
return parseInt(b.toString('hex'), 16)
}
const setInt64 = (int64) => {
const b = Buffer.alloc(8);
const MAX_UINT32 = 0xFFFFFFFF;
const big = ~~(int64 / MAX_UINT32);
const low = (int64 % MAX_UINT32) - big;
b.writeUInt32BE(big, 0); // 00 00 01 53 00 00 00 00
b.writeUInt32BE(low, 4); // 00 00 01 53 36 9a 06 58
return b;
}
```

Which is it? I'm confused because if I understand right, Node.js doesn't support 64 bit integers. Or at least not absolutely.

asked on Stack Overflow Sep 21, 2020 by J.Todd

The max integer in Javascript is `Number.MAX_SAFE_INTEGER`

which is `9,007,199,254,740,991`

which is the largest mantissa value that fits in a double-precision floating point value which is how Javascript stores all numbers.

A double precision floating value is structured like this:

Which has 52 bits for the integer portion, one bit for the sign and 11 bits for the exponent.

Here's a simple test to illustrate:

```
let x = 0xffffffffffffffff;
console.log(x.toString(16))
```

and it will not output the exact 64 bit value that you tried to assign it (it will have been coerced into something that is not the same value).

Beyond the limit of the Number type, you would have to use `BigInt`

which is limitless in what it can store, but takes more storage and is a lot slower for math operations and does not directly interoperate with the Number type.

User contributions licensed under CC BY-SA 3.0