Javascript-bitwise Operator

Source: Internet
Author: User
Tags bitwise operators

1. Not

Bit operators not from ~ The essence of the. Not operator is to calculate a negative number and then subtract 1.

The bitwise operator not is a three-step process.

A. Convert the operator to a 32-bit number.

B. Convert the binary form into its binary anticode

C. Convert the binary back code to a floating point number.

Example:

 
VaR num = 10; document. Write (~ Num );
Result:
-11
 
2. and
Bitwise operators and are expressed by &. The binary format of numbers is calculated directly. The calculation rules are as follows:
First Digit Second digit Result
0 0 0
0 1 0
1 0 0
1 1 1

Example:

VaR num1 = 10;// 1010VaR num2 = 11;// 1011Document. Write (num1 & num2 );

Result:

The binary value of 10 indicates 1010.

3. or

Bitwise operators or operators are represented by symbols. binary operations are performed directly. The rules are as follows:

First Digit Second digit Result
0 0 0
0 1 1
1 0 1
1 1 1

Example:

VaR num1 = 10;// 1010VaR num2 = 11;// 1011Document. Write (num1 | num2 );

Result:

The binary value of 11 is 1011.

4. XOR

The bitwise operator XOR is represented by the symbol ^. binary operations are performed directly. The rules are as follows:

First Digit Second digit Result
0 0 0
0 1 1
1 0 1
1 1 0

Example:

VaR num1 = 10;// 1010VaR num2 = 11;// 1011Document. Write (num1 ^ num2 );

Result:

The binary value of 1 is 1.

5. <

The Left shift operator is represented by <. It moves all digits of a number to the left.

Note:

A. When the number is left shifted, the vacancy on the right of the number is filled by 0, and the result is a complete 32-bit number.

B. The left shift operation retains the digit symbol bit.

Example:

 
Document. Write (10 <2 +"<Br/>"); Document. Write (-10 <2 );

Effect:

6.>

The shifted right operation is represented by>. It shifts all the numbers in the 32-bit number to the right, and retains the symbols of this number.

Note:

A. the symbol bit remains unchanged.

B. When the right shift is performed, the vacancy on the left of the number is filled by 0.

Example:

Document. Write (10> 1); document. Write ("<Br/>"); Document. Write (-10> 1 );

Effect:

 

7. >>>

The unsigned right shift is represented by >>>. It shifts all the numbers in the 32-bit number to the right.

Note:

A. the unsigned right shift operation fills all spaces with 0.

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

C. For negative numbers, the negative number is changed to a positive number after the unsigned right shift due to the left side filling of 0.

For example:

 
Document. Write (-10 >>> 1 );

Result:

Calculation 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

After unsigned 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

Result:

2147483643

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.