Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is use of ~~ in javascript? [duplicate]

Tags:

javascript

Possible Duplicate:
Understanding javascript bitwise NOT

I found it here: front-end-interview-questions question down below. what this code ~~3.14 will return?

I searched on google but didn't found anything on this.

like image 360
Jatinder Avatar asked Sep 26 '12 05:09

Jatinder


1 Answers

~ is the bitwise complement operator in JavaScript (and C/C++ and other languages). You can find more details here: How does the bitwise complement (~) operator work?

In this case:

  • 3.14 is converted from floating point to integer, so it becomes 3.
  • ~3 is -4 because of the Two's Complement representation.
  • Then ~(-4) becomes 3.

Basically, ~n is equal to -n-1 for integers with Two's Complement representation.

like image 133
battery Avatar answered Nov 03 '22 00:11

battery