- 1

Conversion : how the out put come 1 ???

var a=parseInt("130", 2); document.write(a); /* According to my answer came 10 1×2^2 + 3×2^1 + 0×2^0 = 10 */ /*Please understand me briefly*/ https://code.sololearn.com/W5WNcz0AE7s7/?ref=app

19th Oct 2020, 3:38 PM
born2code
born2code - avatar
4 Réponses
+ 4
130 is not a valid binary number expression, binary number only has two valid digits, 0 and 1. The second argument you gave to parseInt was meant as base indicator. When you passed 2, you tell Javascript to treat "130" as a binary number (base 2), but Javascript stop parsing the given string as it finds "3", which is not a valid binary digit. The expression "130" was then understood as just "1" because the incorrect base specification failed the parsing process.
19th Oct 2020, 3:54 PM
Ipang
+ 3
No it doesn't work like that, the second parameter indicates in wich number system the string should be interpreted, 130 is interpreted as a binary number so 130 is bit 1 bit 3? The parser was like what the hell is this ? A bit with a value 3 ? So it discards all bits after 1 and prints only 1.
19th Oct 2020, 3:52 PM
QTWizard
+ 1
Next time please attach the language you need help.
19th Oct 2020, 4:04 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Very very thanx for answering me
19th Oct 2020, 4:31 PM
born2code
born2code - avatar