Bitwise shift left (<<) (Operator/bitwise)

Bitwise shift leftwards one operand according to another.

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 value to be shifted
anOperand2A distance to shift anOperand1

The bitwise shift left operator converts its left operand to a 32 bit integer and moves it leftwards by the number of bits indicated by the right operand.

As the value is shifted leftwards, bits that roll out of the left end of the register are discarded. The right-hand end of the register is filled with zero bits. Shifting leftwards by 32 bits will fill the register with all zero bits.

Because the value is converted to an integer, any fractional part is discarded as the shift begins.

The right-hand operand is converted to a 5 bit value with a bitwise mask to limit the distance of the shift to 32 bits. This can cause unexpected results if the right-hand side is derived from an expression that may yield a value larger than 32.

The associativity is left to right.

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

You can accomplish bitwise shift lefts by multiplying values using powers of 2. Multiplying a value by 2 shifts leftwards by one bit position.

Example code:

   <HTML>
   <HEAD></HEAD>
   <BODY>
   <SCRIPT>
   myValue1 = 0x00FF;
   myValue2 = myValue1 << 4;
   document.write("Val 1 : " + binary32(myValue1) + "<BR>");
   document.write("Result : " + 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, Bitwise shift left then assign (<<=), Bitwise shift operator, Bitwise shift right (>>), Bitwise shift right and assign (>>=), Bitwise unsigned shift right (>>>), Bitwise unsigned shift right and assign (>>>=), Operator Precedence, Shift operator

insert figure 0017

Cross-references:

ECMA 262 edition 2 - section - 11.7.1

ECMA 262 edition 3 - section - 11.7.1