JavaScript Tutorial/Operators/Bitwise Operator

Материал из Web эксперт
Версия от 08:24, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Bitwise AND Truth Table

First Value Second Value Result true true true true false false false true false false false false

^= (Bitwise Exclusive OR Assignment)

Syntax    variable ^= value


Bitwise Exclusive OR Truth Table

First Value Second Value Result true true false true false true false true true false false false

Bitwise Exclusive OR Truth Table

First Value Second Value Result true true false true false true false true true false false false



var iResult = 25 | 3;
alert(iResult);    //outputs "27"
25 = 0000 0000 0000 0000 0000 0000 0001 1001
 3 = 0000 0000 0000 0000 0000 0000 0000 0011
--------------------------------------------
OR = 0000 0000 0000 0000 0000 0000 0001 1011


~ (Bitwise NOT)

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"


Bitwise Operators

Bitwise operators in JavaScript only work with integers that are 32 bits in length.

  1. Bitwise AND
  2. Bitwise OR
  3. Bitwise XOR (exclusive OR)
  4. Bitwise NOT
  5. Shift Left (<<)
  6. Shift Right with Sign(>>)
  7. Shift Right Zero Fill(>>>)

2. 5. Bitwise Operator 2. 5. 1. Bitwise Operators 2. 5. 2. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseANDTruthTable.htm">Bitwise AND Truth Table</a> 2. 5. 3. <A href="/Tutorial/JavaScript/0040__Operators/UsingtheBitwiseANDOperator.htm">Using the Bitwise AND Operator</a> 2. 5. 4. <A href="/Tutorial/JavaScript/0040__Operators/UsingtheBitwiseANDPlusAssignmentOperator.htm">Using the Bitwise AND Plus Assignment Operator</a> 2. 5. 5. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseExclusiveORTruthTable.htm">Bitwise Exclusive OR Truth Table</a> 2. 5. 6. <A href="/Tutorial/JavaScript/0040__Operators/UsingtheBitwiseExclusiveOROperator.htm">Using the Bitwise Exclusive OR Operator</a> 2. 5. 7. <A href="/Tutorial/JavaScript/0040__Operators/UsingtheBitwiseExclusiveOROperator2.htm">Using the Bitwise Exclusive OR Operator (2)</a> 2. 5. 8. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseORAssignment.htm">|= (Bitwise OR Assignment)</a> 2. 5. 9. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseExclusiveORAssignment.htm">^= (Bitwise Exclusive OR Assignment)</a> 2. 5. 10. <A href="/Tutorial/JavaScript/0040__Operators/UsingBitwiseExclusiveORPlusAssignmentOperator.htm">Using Bitwise Exclusive OR Plus Assignment Operator</a> 2. 5. 11. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseOR.htm">| (Bitwise OR)</a> 2. 5. 12. <A href="/Tutorial/JavaScript/0040__Operators/BitwiseNOT.htm">~ (Bitwise NOT)</a> 2. 5. 13. <A href="/Tutorial/JavaScript/0040__Operators/ShiftLeft.htm"><< (Shift Left)</a> 2. 5. 14. <A href="/Tutorial/JavaScript/0040__Operators/ShiftLeftAssignment.htm"><<= (Shift Left Assignment)</a> 2. 5. 15. <A href="/Tutorial/JavaScript/0040__Operators/LessThanorEqual.htm"><= (Less Than or Equal)</a> 2. 5. 16. <A href="/Tutorial/JavaScript/0040__Operators/ShiftRightwithSign.htm">>> (Shift Right with Sign)</a> 2. 5. 17. <A href="/Tutorial/JavaScript/0040__Operators/ShiftRightwithSignAssignment.htm">>>= (Shift Right with Sign Assignment)</a> 2. 5. 18. <A href="/Tutorial/JavaScript/0040__Operators/ShiftRightZeroFill.htm">>>> (Shift Right Zero Fill)</a> 2. 5. 19. <A href="/Tutorial/JavaScript/0040__Operators/ShiftRightZeroFillAssignment.htm">>>>= (Shift Right Zero Fill Assignment)</a>

| (Bitwise OR)

Syntax num1 | num2

Bitwise Exclusive OR Truth Table

First Value Second Value Result true true true true false true false true true false false false

|= (Bitwise OR Assignment)

<html>
    <script language="JavaScript">
    <!--
    // 2 = 00000000000000000000000000000011
    // 5 = 00000000000000000000000000000101
    // 7 = 00000000000000000000000000000111
    x = 2;
    x |= 5;
    document.write("x = ",x);
    -->
    </script>
</html>


<html>
    <script language="JavaScript">
    <!--
    str = new String("34");
    if(str <= 77)
      document.write("34 is less than or equal to 77");
    else
      document.write("Returned FALSE!");
    -->
    </script>
</html>


The shift left operator looks at the integer to the left of the operator as a 32-bit binary number.

The number of positions specified by num2 shifts all the bits of num1 to the left.

As the bits are shifted to the left, zeros are filled in on the right.

Because the number can only be 32-bits long, the extra bits on the left are lost.

The 32-bit binary result of the shifting operation is converted to an integer value and returned from the shift left operation.

The result generated from the shift left operator can be quickly calculated by multiplying the number by 2 raised to the x power, where x is the number of positions shifted.



<html>
    <script language="JavaScript">
    <!--
    // 2 = 00000000000000000000000000000010
    // 8 = 00000000000000000000000000001000
    x = 2 << 2;
    document.write("2 << 2 = ",x);
    -->
    </script>
    </html>


The shift left operator looks at the integer stored in the variable to the left of the operator as a 32-bit binary number.

All the bits in this number are shifted to the left by the number of positions specified by the integer to the right of the operator.

As the bits are shifted to the left, zeros are filled in on the right.

The extra bits on the left are lost.

The 32-bit binary result of shifting operation is converted to an integer value and stored in the variable to the left of the operator.



<html>
    <script language="JavaScript">
    <!--
    //  3 = 00000000000000000000000000000011
    // 12 = 00000000000000000000000000001100
    x = 3;
    x <<= 2;
    document.write("x = ",x);
    -->
    </script>
</html>


>> (Shift Right with Sign)

The shift right with sign operator looks at the integer to the left of the operator, num1, as a 32-bit binary number.

All the bits in this number are shifted to the right by the number of positions specified by num2.

If the original number is positive, zeros are added to the left side of the binary number.

If the original number is negative, ones are used.

The extra bits on the right are lost.

The 32-bit binary result of shifting operation is converted to an integer value and returned from the shift right with sign operation.

The result generated from the shift right with sign operator can be quickly calculated by dividing the number by 2 raised to the x power, where x is the number of positions shifted. Discard the remainder.



<html>
    <script language="JavaScript">
    <!--
    // 14 = 00000000000000000000000000001110
    //  3 = 00000000000000000000000000000011
    x = 14 >> 2;
    document.write("14 >> 2 = ",x);
    document.write("<br>");
    // -4 = 11111111111111111111111111111100
    // -2 = 11111111111111111111111111111110
    y = -4 >> 1;    //y is equal to -2
    document.write("-4 >> 2 = ",y);
    -->
    </script>
    </html>


>>= (Shift Right with Sign Assignment)

The shift right with sign and assignment operator looks at the integer to the left of the operator as a 32-bit binary number.

All the bits in this number are shifted to the right by the number of positions specified by the integer to the right of the operator.

If the original number is positive, zeros are added to the left side of the binary number.

If the original number is negative, ones are used.

Because the result can only be 32-bits long, the extra bits on the right are lost.

The 32-bit binary result of shifting operation is converted to an integer value and stored in the variable to the left of the operator.



<html>
    <script language="JavaScript">
    <!--
    // 15 = 00000000000000000000000000001111
    //  7 = 00000000000000000000000000000111
    x = 15;
    x >>= 1;
    document.write("x = ",x);
    -->
    </script>
</html>


>>> (Shift Right Zero Fill)

The shift right zero fill operator looks at the integer to the left of the operator as a 32-bit binary number.

All the bits in this number are shifted to the right by the number of positions specified by the integer to the right of the operator.

As the bits are shifted to the right, zeros are filled in on the left, regardless of the sign of the original integer.

The extra bits on the right are lost.

The 32-bit binary result of this shifting operation is converted to an integer value and returned from the shift right zero fill operation.



<html>
    <script language="JavaScript">
    <!--
    // 13 = 00000000000000000000000000001101
    //  6 = 00000000000000000000000000000110
    x = 13 >>> 1;
    document.write("13 >>> 1 = ",x);

    document.write("<br>");
    //         -8 = 11111111111111111111111111111000
    // 1073741822 = 00111111111111111111111111111110
    y = -8 >>> 2;
    document.write("-8 >>> 2 = ",y);
    -->
    </script>
    </html>


>>>= (Shift Right Zero Fill Assignment)

The shift right zero fill with assignment operator looks at the integer to the left of the operator as a 32-bit binary number.

All the bits in this number are shifted to the right by the number of positions specified by the integer to the right of the operator.

As the bits are shifted to the right, zeros are filled in on the left, regardless of the sign of the original integer.

The extra bits on the right are lost.

The 32-bit binary result of this shifting operation is converted to an integer value and stored in the variable to the left of the operator.



<html>
    <script language="JavaScript">
    <!--
    //         -6 = 11111111111111111111111111111010
    // 1073741822 = 00111111111111111111111111111110
    x = -6;
    x >>>= 2;
    document.write("x = ",x);
    -->
    </script>
</html>


Using Bitwise Exclusive OR Plus Assignment Operator

<html>
    <script language="JavaScript">
    <!--
    // integer = 32-bit binary representation
    // 10 = 00000000000000000000000000001010
    //  6 = 00000000000000000000000000000110
    // 12 = 00000000000000000000000000001100
    x = 10;
    x ^= 6;
    document.write("x = ",x);
    -->
    </script>
</html>


Using the Bitwise AND Operator

<html>
    <script language="JavaScript">
    <!--
    // integer = 32-bit binary representation
    // 11 = 00000000000000000000000000001011
    //  6 = 00000000000000000000000000000110
    //  2 = 00000000000000000000000000000010
    answer = 11 & 6;
    document.write("11 & 6 = ",answer);
    -->
    </script>
</html>


Using the Bitwise AND Plus Assignment Operator

var iResult = 25 & 3;
alert(iResult);    //outputs "1"
 25 = 0000 0000 0000 0000 0000 0000 0001 1001
  3 = 0000 0000 0000 0000 0000 0000 0000 0011
---------------------------------------------
AND = 0000 0000 0000 0000 0000 0000 0000 0001


Using the Bitwise Exclusive OR Operator

var iResult = 25 ^ 3;
alert(iResult);    //outputs "26"
25 = 0000 0000 0000 0000 0000 0000 0001 1001
  2 = 0000 0000 0000 0000 0000 0000 0000 0011
---------------------------------------------
XOR = 0000 0000 0000 0000 0000 0000 0001 1010


Using the Bitwise Exclusive OR Operator (2)

<html>
    <script language="JavaScript">
    <!--
    // integer = 32-bit binary representation
    //  9 = 00000000000000000000000000001001
    //  5 = 00000000000000000000000000000101
    // 13 = 00000000000000000000000000001101
    document.write("9 | 5 = ", (9 | 5) );
    -->
    </script>
    </html>