·  JavaScript – Syntax – Operators – Bitwise

 

- Info:

  - This tutorial shows how to use Bitwise Operators: & | ^ ~ << >> >>>.

 

- Example:

  - Create D\Temp\JavaScript\Test.html

 

    Test.html

    <script type="text/javascript" language="javascript">

      

      left        = 27;            //11011

      right       = 18;            //10010

      

      and         =  left & right; //10010     1 if both   bits are 1.

      or          =  left | right; //11011     1 if either bit  is  1.

      xor         =  left ^ right; //01001     1 if        bits are different.

      invert      = ~left;         //-11100    Invert bits.

      shiftLeft   =  left <<  3;   //11011000  Shift bits to the left  by 3. Fill with 0.

      shiftRight  =  left >>  2;   //110       Shift bits to the right by 2. Fill with 0.

      shiftRight2 =  left >>> 2;   //110 

      

      window.alert(and        .toString(2));

      window.alert(or         .toString(2));

      window.alert(xor        .toString(2));

      window.alert(invert     .toString(2));

      window.alert(shiftLeft  .toString(2));

      window.alert(shiftRight .toString(2));

      window.alert(shiftRight2.toString(2));

   

    </script>