Javascript bitwise operators

Source: Internet
Author: User
Operator Usage Description
Bitwise AND A & B If both operands have 1 bits, 1 is returned.
By bit or A | B If the corresponding bits of both operands are 0, 0 is returned for this bit.
Bitwise OR A ^ B If the two operands have only one 1 bit, 1 is returned.
Reverse Lookup ~ A Reverses each bit of the operand.
Move left A <B Shifts the binary form of a to the left of B. Fill zero in the right.
Arithmetic shift right A> B Shifts the binary form of a to the right of B. Ignore the removed bits.
Right Shift of Logic A >>> B Shifts the binary form of a to the right of B. Ignore the removed bits and add 0 to the left.

Binary Operators

Includes three types: &, |, and ^, for example:

Result 15 & 9 is 9 (1111 & 1001 = 1001)

 

15 | 9 is 15 (1111 | 1001 = 1111)

 

15 ^ 9 is 6 (1111 ^ 1001 = 0110)

 

Shift Operator
The shift operator requires two operands: the first is the value to be shifted, and the second is the number to be shifted to the first number. The direction of the shift is determined by the operator used.

The shift operator converts two operators to 32-bit integer values and returns results of the same type as the left operand.

<(Left shift)
This operator shifts the first operand to several places on the left. The removed bits are ignored. Fill in the right blank.
For example, 9 <2 results in 36 because 1001 shifts to the left to 100100, Which is 36.

>>( Right shift of arithmetic)
This operator will remove several bits from the first operand. The removed bits are ignored. The left-side fill is the same as the original leftmost position.
For example, the result of 9> 2 is 2, because 1001 shifted to 10, which is 2. Otherwise,-9> 2 returns-3, because the symbol bit must be taken into account.

>>> (Logical right shift)
This operator will remove several bits from the first operand. The removed bits are ignored. Fill in the left-side empty space.
For example, 19> 2 results in 4, because 10011 shifted to 100, which is 4. For non-negative numbers, the result of right-shift arithmetic is the same as that of right-shift logic.

Pay special attention to the difference between logical right shifting and arithmetic right shifting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.