Bitwise AND (&) (Operator/bitwise)

Bitwise AND of two operands.

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:-anOperand1 & anOperand2
Argument list:anOperand1A binary bit pattern
anOperand2Another binary bit pattern

The result is the bitwise AND of both binary bit pattern values.

This operator performs a bit by bit AND of the 32-bit value derived from both operands. Effectively, each corresponding bit pair has a logical AND applied to it.

The truth table shows the result of this operator for two Boolean primitive values:

ABAND
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

Where a corresponding bit is 1 in both values, a 1 bit is inserted into the result, otherwise the value is zero.

The associativity is left to right.

Refer to the Operator Precedence topic for details of execution order.

Example code:

   <HTML>
   <HEAD></HEAD>
   <BODY>
   <SCRIPT>
   myValue1 = 0xFFFF;
   myValue2 = 0xFF00;
   myValue3 = myValue1 & myValue2;
   document.write("Val 1 : " + binary32(myValue1) + "<BR>");
   document.write("Val 2 : " + binary32(myValue2) + "<BR>");
   document.write("AND : " + binary32(myValue3) + "<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, Binary bitwise operator, Bit-field, Bitwise AND then assign (&=), Bitwise expression, Bitwise operator, Logical AND (&&), Operator Precedence

insert figure 0029

Cross-references:

ECMA 262 edition 2 - section - 11.10

ECMA 262 edition 2 - section - 11.13

ECMA 262 edition 3 - section - 11.10