A brief analysis of the application of JavaScript bitwise-Operator

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

  below according to their own understanding of a simple talk about the use of bits in JS (the same applies to other languages), if there are errors, please correct me

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, we have to mention what is "bit": the number or character is stored in memory as a sequence of 0 and 1, each 0 and 1 is called 1 bits, for example, 10 data 2 is stored in the computer as 0 0 0 0 0 0 1, and when we change the bit value in memory, the value represents a different meaning. For example, to move 2 before a bit, now the storage unit has become 0 0 0 0 0 1 0 0, this value represents the decimal 4, which is the operating principle of bitwise operators.   Bitwise operators have 6 & bitwise with | bitwise OR ^ bitwise XOR-Reverse >> right-shift << left-shift     1 & operator & is a two-element operator that combines the corresponding bits in the operands in a particular way If the corresponding bit is 1, then the result is 1, if any one bit is 0 then the result is 0 1 & 3 results to see how it works: the binary representation for 1 0 0 0 0 0 0 1 is the binary representation of 3 0 0 0 0 0 1 According to the rules of the & The results are 0 0 0 0 0 0 0 1, the decimal representation is 1   as long as any one is 0 & the result of the operation is 0, so you can use & to set a variable to 0 unnecessary bits, such as a variable of the binary representation of 0 1 0 0 1 0 0-1, I would like to protect Stay low 4, eliminate high 4-bit with & 0x0f on it (live: 0x0f is a 16-in notation, corresponding binary is 0 0 0 0 1 1, 1 1), this feature has a very important application, which 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 results for the 3   3 ^ operator ^ operator with | Similarly, but one difference is that if two operating bits are 1, the result is 0 0 1 0 0 0 0 0, 1 0 1 0 1 1 0 produces 1 0 0 0 0 1 1 0   1 ~ Operators ~ is the bitwise1 to 0, 0 to 1   5 shift operator shift operators move a bit to the left or right << to the left with a specified value and >> move to the right, over bits will be lost, while the vacated bits are 0   such as 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 (decimal 16387) move to the left two will turn 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 (decimal 12) to the right two digits are 0 0 0 1 0 0 0 0 0 0 0 0 0 0 (decimal 4096)     below describes some of the specific applications mentioned above 2 move forward 1 bits into 4 Use this feature to do multiplication 2 << 1 =4 3 << 1 = 6 4 << 1 = 8 Same as >> you can do division &N Bsp   Any decimal place it >> 0 can be rounded as 3.14159 >> 0 = 3;   ^ The operator has a magical feature such as the following 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.