Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do these binary representations yield the same number?

According to the documentation, one can convert a binary representation of a number to the number itself using parseInt(string, base).

For instance,

var number = parseInt("10100", 2);
// number is 20

However, take a look at the next example:

var number = parseInt("1000110011000011101000010100000110011110010111111100011010000", 2);
// number is 1267891011121314000

var number = parseInt("1000110011000011101000010100000110011110010111111100100000000", 2);
// number is 1267891011121314000

How is this possible?

Note that the binary numbers are almost the same, except for the last 9 bits.

like image 344
Matias Cicero Avatar asked Dec 08 '25 16:12

Matias Cicero


1 Answers

1267891011121314000 is way over Number.MAX_SAFE_INTEGER (9007199254740991). It can't safely represent it in memory.

Take a look at this classic example:

1267891011121314000 == 1267891011121314001  // true
like image 172
Derek 朕會功夫 Avatar answered Dec 10 '25 04:12

Derek 朕會功夫



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!