Bitwise NOT - complement (~) (Operator/bitwise)

Bitwise NOT of one operand.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Number primitive
JavaScript syntax:-~anOperand
Argument list:anOperandA numerical value

The operand is evaluated and then converted to a 32-bit integer value. Every bit is complemented and the result is a bitwise NOT.

The truth table shows the result of this operator for a Boolean primitive value:

ANOT
falsetrue
truefalse

This operation is applied to each individual bit in the operand, inverting them one by one.

Note that this could be classified as a unary operator but here we have called it a bitwise operator on account of its functionality rather than its placement.

The associativity is right to left.

Refer to the operator precedence topic for details of execution order.

Warnings:

Example code:

   <HTML>

   <HEAD></HEAD>

   <BODY>

   <SCRIPT>

   

   myValue1 = 0xFFFF;

   myValue2 = ~myValue1

   document.write("Val 1 : " + binary32(myValue1) + "<BR>");

   document.write("NOT : " + binary32(myValue2) + "<BR>");

   // Binary convertor (ignore sign bit on MSIE)

   function binary32(aValue)

   {

     myArray = new Array(32);

   

     for(myEnum=0; myEnum<32; myEnum++)

     {

       if(aValue & Math.pow(2, myEnum))

       {

         myArray[31-myEnum] = "1";

       }

       else

       {

         myArray[31-myEnum] = "0";

     }

   }

   return myArray.join("");

   }

   

   </SCRIPT>

   </BODY>

   </HTML>

See also:Associativity, Bit-field, Logical NOT - complement (!), Operator Precedence, Unary operator

insert figure 0028

Cross-references:

ECMA 262 edition 2 - section - 11.4.8

ECMA 262 edition 3 - section - 11.4.8