Possible duplicate:Understanding in different ways javascript NOT
I found it here: the question is before the interview questions below. What will this code ~~ 3.14 return?
I searched on google but found nothing on this.
It will return 3. ~represents the bitwise NOT operator in JavaScript.
~
Basically, ~ 3.14 matches ~ 3, which is ~ 011 in binary => 100 or 4 in base 10. ~ 4 or ~ 100 is 011 or 3 in base 10.
~ - JavaScript ( C/++ ). : (~)?
:
3.14
3
~3
-4
~(-4)
, ~n -n-1 Two Complement.
~n
-n-1
32- .
MDN:
x -(x + 1). , ~5 -6.
x
-(x + 1)
~5
-6
In your case:
~~3.14 = -((~3.14) + 1) = -(-(3.14 + 1) + 1) = -(-(3 + 1) + 1) = -(-4 + 1) = -(-3) = 3