This question is simply to curiosity.
Via console
parseInt(1111111111111111,2) // 16 1's
returns
65535
and
parseInt(11111111111111111,2) // 17 1's
returns
65535
Also
// 16 1's 17 1's
if(parseInt(1111111111111111,2) === parseInt(11111111111111111,2))
returns
true
Where is the trick?
Thanks
11111111111111111
is a decimal number literal for a number that is too large for Javascript to encode precisely.
parseInt(, 2)
will stop at the first character that isn't 1
or 0
, so it stops parsing once the precision degrades.
Change that to a string and you'll be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With