On the computational method of bit operators

Source: Internet
Author: User

We all know that bit operators are faster than normal operators, but why are they fast? First of all, our normal operators are also implemented by the computer through a bitwise operation Call stack. When the bitwise operator is evaluated, the operand is first converted to 2.

So what are the JavaScript bitwise operators?

1. & Bitwise AND

2. | Bitwise OR

3. ^ Bitwise XOR OR

4. ~ Take Back

5. >> bitwise LEFT Shift

6. >> Bitwise RIGHT Shift

Description

(1) The bitwise operator, except for ~, is a two-mesh operator, which requires an operand on each side.

(2) The operation can only be integer or character data, and cannot be a real data type.

1. Bitwise AND &

True if both the left and right values are true, otherwise false.

Console.log (5&7);//5

Detailed

2. Bitwise OR |

True if both the left and right values are true, otherwise false.

Console.log (5|7);//7

Detailed

3. Bitwise XOR ^

If the two binary numbers participating in the calculation are the same number, then it is 0 and the XOR is 1.

Console.log (true ^ true);//0

Console.log (true ^ false);//1

Console.log (false ^ false);//0

Console.log (false ^ true);//1

Detailed

4. Bitwise COUNTER ~

Console.log (~2.5);//-3 takes the inverse and then rounding down

5. Bitwise LEFT SHIFT <<

Explanation: Example: 5<<2 binary 5 to the left two bits, 0 on the right, also equivalent to 5*2^2, down rounding.

Console.log (5<<2);//20 5*x2*2=20

6. Bitwise RIGHT SHIFT >>

Explanation: Example: 5>>2 binary 5 to the right two bits, left 0, also equivalent to 5/2^2, down rounding.

Console.log (5>>2);//1 5*/(2*2) =1.25

On the computational method of bit operators

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.