Javascript, how does it work string to int use bitshift like '2' >> 0 -
i use bitshift want conver string bit, want know how work.
"12345" >> 0 // int 12345
found '>>' deal 32-bit things , return int. confuse how work?
according the specification, in "12345" >> 0
, happens:
"12345"
put through specification's abstracttoint32
function:- toint32 runs through
tonumber
coerce string standard javascript number (an ieee-754 double-precision binary floating-point value); ends being 12345 toint32
converts 32-bit integer value; ends being...12345
- toint32 runs through
- that 32-bit integer shifted...not @ because used
>> 0
, still have 12345
and return int
sort of. if stored result in variable, end being standard javascript number again (a double). spec seems allow possibility int value used as-is if combined operation others, though, makes sense.
if goal force "12345"
number using default coercion rules, >> 0
obscure , inefficient way (not latter matter). unary +
rather more direct , applies same rules: +"12345"
Comments
Post a Comment