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:

  1. "12345" put through specification's abstract toint32 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
  2. 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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -