JavaScript Tutorial/Operators/Bitwise Operator

Материал из Web эксперт
Перейти к: навигация, поиск

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)

   <source lang="javascript">

Syntax variable ^= value</source>


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



   <source lang="javascript">

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</source>


~ (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.



   <source lang="javascript">

var iNum1 = 25; //25 is equal to 00000000000000000000000000011001 var iNum2 = ~iNum1; //convert to 111111111111111111111111111100110 alert(iNum2); //outputs "-26"</source>


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)

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


>> (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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

>>= (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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


>>> (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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>
   
  

>>>= (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.



   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


Using Bitwise Exclusive OR Plus Assignment Operator

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


Using the Bitwise AND Operator

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>

</html></source>


Using the Bitwise AND Plus Assignment Operator

   <source lang="javascript">

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</source>


Using the Bitwise Exclusive OR Operator

   <source lang="javascript">

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</source>


Using the Bitwise Exclusive OR Operator (2)

   <source lang="javascript">

<html>

   <script language="JavaScript">
   
   </script>
   </html></source>