Analysis on the Application of JavaScript bitwise operators

Source: Internet
Author: User
Tags bitwise operators

Analysis on the Application of JavaScript bitwise operators

Next, let's talk about the bit operations in js (also applicable to other languages) based on your own cognition. If there are any errors, please correct me.

Most languages provide bitwise operators. bitwise operators are widely used in c, c ++, and other languages. There are not many application examples in JS, AS, and other scripting languages, sometimes, the proper use of the bitwise operator will achieve good results.

Next, let's talk about the bit operations in js (also applicable to other languages) based on your own cognition. If there is an error, please let me know.

 

Bitwise operators regard operands as a series of separate bits, rather than a numerical value. So before that, we have to mention what is "bit ":

Values or characters are stored in the memory as sequences of 0 and 1. Each 0 and 1 is called a single digit, for example, 10-digit data 2 is stored in a computer as 0 0 0 0 0 0 1 0. When we change the bit value in the memory, the meaning of this value changes, for example, to move 2 forward to the first place, the storage unit is now 0 0 0 0 0 1 0 0. This value indicates 4 in decimal format, this is the operating principle of the bitwise operator.

 

There are 6 bitwise operators

& Bitwise AND

| By bit or

^ Bitwise OR

~ Invert

> Right shift

<Move left

 

 

1 & operator

& Is a binary operator. It combines the corresponding bits in the operand in a specific way. If the corresponding bits are all 1, the result is 1. If any one of the bits is 0, the result is 0.

The result of 1 & 3 is 1.

To see how it runs:

The binary value of 1 is 0 0 0 0 0 1

The binary value of 3 is 0 0 0 0 1 1

The result obtained based on the & rule is 0 0 0 0 0 0 1, which is 1 in decimal format.

 

As long as any bit is 0 and the result of the operation is 0, you can use & to set unnecessary bits of a variable to 0, for example, if the binary value of a variable is 0, 0, 0, 0, and 1, I want to keep four lower digits, and use & 0x0F to remove four higher digits: 0x0F is a hexadecimal notation, and the corresponding binary value is 0 0 0 0 1 1 1 1 1). This feature is very important and will be mentioned later.

 

2 | Operator

| The difference with & is that if any of the corresponding bits is 1, the result is 1.

1 | 3. The result is 3.

 

3 ^ Operator

^ The operator is similar to |, but the difference is that if both operation bits are 1, the result is 0.

0 1 0 0 0 0 1

0 1 0 1 1 0 1 0

0 0 0 1 1 0 1 1

 

4 ~ Operator

~ Is the counterpoint. 1 is changed to 0, and 0 is changed to 1.

 

5. Shift Operator shift operator shifts the bit to the left or right based on the specified value

<Move to left> move to the right. If it is exceeded, the bits are lost, and the empty bits are 0.

 

For example, 0 1 0 0 0 0 0 0 0 0 1 1 1 (decimal 16387) move two digits to the left to change

0 0 0 0 0 0 0 0 0 0 1 1 0 0 (decimal 12)

Move two digits to the right.

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 (decimal 4096)

 

 

The following describes some specific applications.

As mentioned above, 2 moves the first digit forward to 4. This feature can be used for multiplication.

2 <1 = 4

3 <1 = 6

4 <1 = 8

Likewise> division can be performed.

 

 

Any decimal number can integer it> 0

For example, 3.14159> 0 = 3;

 

^ Computing has a magical feature

As shown in the following code:

 

 

The Code is as follows:

<Script>

Var n1 = 3;

Var n2 = 4;

N1 ^ = n2;

N2 ^ = n1;

N1 ^ = n2;

</Script>

 

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.