Skip to content

Instantly share code, notes, and snippets.

@d-simon
Created March 9, 2014 17:25
Show Gist options
  • Save d-simon/9451165 to your computer and use it in GitHub Desktop.
Save d-simon/9451165 to your computer and use it in GitHub Desktop.
Double Bitwise NOT Operator (~~, double tilde)

Taken from Tilde or the Floor? Javascript bitwise operators in Practice

#Bitwise NOT - The ~ operator

Apart from "inverting the bits of its operand" bitwise NOT in JavaScript is actually very useful not only when it comes to binary. Firstly, it has a very interesting effect on integers - it converts the integer to -(N+1) value.

For example:

~2 === -3; //true
~1 === -2; //true
~0 === -1; //true
~-1 === 0; //true

However, the most practical way of utilizing the power of this operator is to use it as a replacement for Math.floor() function as double bitwise NOT performs the same operation a lot quicker. You can use it, to convert any floating point number to a integer without performance overkill that comes with Math.floor(). Additionally, when you care about minification of your code, you end up using 2 characters (2 tildes) instead of 12.

Example use of double tilde / double bitwise NOT operator:

~~2 === Math.floor(2); //true, 3
~~2.4 === Math.floor(2); //true, 2
~~3.9 === Math.floor(3); //true, 3   
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment