javascript--Bitwise operators

Source: Internet
Author: User
Tags bitwise bitwise operators

1.NOT

The bitwise operator is not represented by ~. The essence of the not operator is to negate the number and then subtract 1.

The bitwise operator NOT is a three-step process.

A. Converting an operator to a 32-digit number

B. Converting binary form to its binary counter code

C. Converting binary inverse codes to floating point numbers

Example:

        var num=10;        document.write (~num);
Results:
-11
2.AND
The bitwise operator and is represented by &. Directly to the binary form of the number. The arithmetic rules are as follows:
First number A second number Results
0 0 0
0 1 0
1 0 0
1 1 1

Example:

        var num1=10;

Results:

Binary representation of 10 1010

3.OR

The bitwise operator or is represented by a symbol |. directly to the binary, the rules are as follows:

First number A second number Results
0 0 0
0 1 1
1 0 1
1 1 1

Example:

        var num1=10;

Results:

The binary representation of 11 is 1011

4.XOR

The bitwise operator XOR is represented by the symbol ^. Direct binary operation. The rules are as follows:

First number A second number Results
0 0 0
0 1 1
1 0 1
1 1 0

Example:

        var num1=10;

Results:

The binary representation of 1 is 1

5.<<

The left shift operator is represented by <<. It moves all digits in the number to the left by the specified number.

Attention:

A. When you move the digits to the left, the empty space on the right side of the number is filled with zeros so that the result is a full 32-digit number

B. The left shift retains the sign bit of the number.

Example:

        document.write (10<<2+"<br/>");        document.write ( -10<<2);

Effect:

6.>>

A signed right-shift operation is represented by >>. It shifts all the numbers in the 32-digit number to the right. The symbol for that number is preserved at the same time.

Attention:

A. The sign bit remains the same

B. When you shift the digits to the right, the left seat of the number is filled by 0

Example:

        document.write (10>>1);        document.write ("<br/>");        document.write ( -10>>1);

Effect:

7.>>>

Unsigned right shift is represented by >>>. It shifts all the numbers in a 32-digit number to the right.

Attention:

A. Unsigned right-shift operation fills all vacancies with 0.

B. For integers, the unsigned right shift is the same as the signed right shift result.

C. For negative numbers, because the left side of 0, resulting in negative numbers after the unsigned right shift, becomes a positive number

For example:

       document.write ( -10>>>1);

Results:

Operation Process:

-10

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0

Unsigned Right Shift -10>>>1

0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1

Results:

2147483643

javascript--Bitwise operators

Related Article

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.