~ (Bitwise NOT) : Bitwise Operator : Operators JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Operators » Bitwise Operator »

 

~ (Bitwise NOT)










The bitwise NOT is represented by a tilde (~).

The bitwise NOT is a three-step process:


  1. The operand is converted to a 32-bit number.

  2. The binary form is converted into its one's complement.

  3. The one's complement is converted back to a floating-point number.








var iNum1 = 25;        //25 is equal to 00000000000000000000000000011001
var iNum2 = ~iNum1;    //convert to 111111111111111111111111111100110
alert(iNum2);          //outputs "-26"














<html>
    <script language="JavaScript">
    <!--
    // integer = 32-bit binary representation
    //  2 = 00000000000000000000000000000010
    // -3 = 11111111111111111111111111111101
    // -2 = 11111111111111111111111111111110
    // -1 = 11111111111111111111111111111111
    document.write("~2 = ",(~2));   //Displays -3
    -->
    </script>
    </html>







HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Operators
» Bitwise Operator