zeros if we're not using binary? Why would we ever care for binary AND, OR, XOR or left/right shift?
I am not going to explain how these operators are affecting binary numbers because there are plenty of resources available covering this topic.
Bitwise NOT - The ~ operator
~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, 2 ~~2.4 === Math.floor(2); //true, 2 ~~3.9 === Math.floor(3); //true, 3
Performance difference converting a list of 100000 float numbers to integer:
|#||Browser||Math.floor()||Bitwise double NOT ~~|
This test is also available here: http://jsperf.com/jsfvsbitnot.
Use double bitwise NOT when:
1. You want to convert the number from float to integer.
2. You want to perform same operation as Math.floor() but a lot quicker.
3. You want to minimalize your code.
Do not use double bitwise NOT when:
1. You run Google Chrome (apparently?).
2. You care about readability of your code.