~ (Bitwise NOT) : Bitwise Operator « Operators « JavaScript Tutorial






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>








2.5.Bitwise Operator
2.5.1.Bitwise Operators
2.5.2.Bitwise AND Truth Table
2.5.3.Using the Bitwise AND Operator
2.5.4.Using the Bitwise AND Plus Assignment Operator
2.5.5.Bitwise Exclusive OR Truth Table
2.5.6.Using the Bitwise Exclusive OR Operator
2.5.7.Using the Bitwise Exclusive OR Operator (2)
2.5.8.|= (Bitwise OR Assignment)
2.5.9.^= (Bitwise Exclusive OR Assignment)
2.5.10.Using Bitwise Exclusive OR Plus Assignment Operator
2.5.11.| (Bitwise OR)
2.5.12.~ (Bitwise NOT)
2.5.13.<< (Shift Left)
2.5.14.<<= (Shift Left Assignment)
2.5.15.<= (Less Than or Equal)
2.5.16.>> (Shift Right with Sign)
2.5.17.>>= (Shift Right with Sign Assignment)
2.5.18.>>> (Shift Right Zero Fill)
2.5.19.>>>= (Shift Right Zero Fill Assignment)