A brief analysis of the application of JavaScript bitwise-Operator _JAVASCRIPT Skills

Source: Internet
Author: User
Tags bitwise bitwise operators numeric value

Most languages provide bitwise operators, which are widely used in languages such as c,c++, while there are not many application examples in scripting languages such as Js,as, and the appropriate use of bitwise operators can be a good result.
Below according to own cognition simple talk about the bit operation use in JS (same applies to other languages), if have mistake, welcome correct correction.

Bitwise operators treat operands as a series of individual bits, not as a numeric value. So before that, you have to mention what a "bit" is:
Values or characters are stored in memory as a sequence of 0 and 1, each 0 and 1 are called 1 bits, for example, 10 data 2 is stored in the computer as 0 0 0 0 0 0 1 0, and when we change the bit value in memory, the value represents a change, such as moving 2 forward, Now the storage unit has become 0 0 0 0 0 1 0 0, this value represents the decimal 4, which is the operation principle of bitwise operator.

Bitwise operators have 6
& Bitwise AND
| bitwise OR
^ per-bitwise XOR OR
~ Take the Counter
>> Move Right
<< Move Left


1 & operator
& is a two-dollar operator that combines the corresponding bits of the operand in a particular way if the corresponding bit is 1, the result is 1, and if any one bit is 0, the result is 0.
1 & 3 Results for 1
To see how it works:
The binary representation of 1 is 0 0 0 0 0 0 1
The binary representation of 3 is 0 0 0 0 0 1 1
According to the & rules, the result is 0 0 0 0 0 0 0 1, the decimal representation is 1

As long as any one of the 0 & operations results is 0, so you can use & to set a variable of the unnecessary bits to 0, such as the binary of a variable for 0 1 0 0 1 0 0 1, I would like to keep a low 4, eliminate high 4 bits with & 0x0f on it (live: 0x0f is 16) notation, the corresponding binary is 0 0 0 0 1 1 1 1), and this feature has a very important application that will be mentioned later.

2 | Operator
| The difference with & is that if either operand in the corresponding bit is 1 then the result is 1.
1 | 3 of the results are 3

3 ^ operator
^ Operators with | Similar, but there is a difference is that if two operating bits are 1, the result produces 0
0 1 0 0 0 0 0 1
0 1 0 1 1 0 1 0
Produces 0 0 0 1 1 0 1 1

4 ~ operator
~ is to the position of the reverse 1 change 0, 0 to 1

5 shift operator shift operator moves a bit to the left or right by the specified value
<< move left and >> move to the right, more than the bit will be lost, and the vacated bit is 0

For example 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 (decimal 16387) move to the left two digits will become
0 0 0 0 0 0 0 0 0 0 0 1 1 0 (decimal 0)
Move to the right two digits is
0 0 0 1 0 0 0 0 0 0 0 0 0 0 (decimal 0)


Here are some specific applications
mentioned above 2 move forward 1 bits into 4 Use this feature to do multiplication operations
2 << 1 =4
3 << 1 = 6
4 << 1 = 8
Empathy >> can do division operations


Any decimal place it >> 0 can be rounded
such as 3.14159 >> 0 = 3;

^ The operator has a magical feature
such as the following code

Copy Code code 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.