JavaScript bit operator

Source: Internet
Author: User
There are 7 bitwise operators in JavaScript, namely & amp;, |, ^, and ,~ , & Lt;, & gt;, & gt; (C # does not have this operator, but C # can use & gt; <G id = "1"> </G>. There are a total of 7 bitwise operators in JavaScript, which are &, |, ^, and ,~ , <, >>, >>> (C # does not have this operator, but C # can use the> logical right shift to implement this operation ), bitwise operations are performed in binary format.

Bitwise AND operator (&)

If the two numbers have the same bit of 1, 1 is returned. Otherwise, 0 is returned. For example, 1 & 2 = 0010 is represented as 0000 in binary format, and is returned in binary format.

Bitwise OR operator (|)

If two numbers have different digits, 1 is returned. Otherwise, 0 is returned, for example, 1 | 2 = 3.

Bitwise XOR or operator (^)

If only one of the two numbers has the same bit is 1, 1 is returned. Otherwise, 0 is returned, for example, 1 ^ 2 = 3.

Bitwise non-operator (~)

~ Is a unary operator that returns the inverse of all digits. Here, we must first describe the storage of negative numbers. Negative numbers are stored by the binary complement code of their positive numbers. Therefore, we must correctly obtain the binary encoding of negative numbers, that is, the binary complement of its positive number. The complement code is obtained and then added with 1. The following is an example:

Calculate the anti-code of 3 first: the binary form of 3 is 00000011, its anti-code is 11111100, and its complement code is 11111101, so the binary code of-3 is 11111101, so we need ~ -3 is to obtain its anticode, Which is 00000010. This is the anticode of-3, which is converted to 2 in decimal format.

If you try multiple times, you will find that the anticode of a number is the opposite of its decimal number minus 1.

Left shift operator (<)

The Left shift operator shifts all the bits of a number to the left. The first and second bits are changed to the second bits... The new blank space is supplemented with 0. For example, 1 <2 = 4,-1 <2 =-4. If the binary value of 1 is 0000 0001, it is changed to 0000 0100 when it is moved to the left two places. If it is converted to decimal, the binary value of-1 is 1111 1111, move it to the left and get a negative number starting with 1111 1111. If you convert it to decimal, You need to perform a re-code calculation, that is, first subtract 1 and get 1011, then, obtain 0000 0100, convert it to decimal plus minus sign, and get 4. Here you need to pay attention to the following binary subtraction algorithm: 1-1 = 0-0 = 0-0 = 1-0 = 1 0-1 = 1 (borrow from the high bit ).

Here we can see that the Left shift operation is to multiply the decimal number by the second digit to the power.

Shifted right operator with symbols (>)

Since the Left shift is multiplied by 2, the right shift must be divided by 2. In fact, this is the case. If the number itself is a positive number, add 0 at a high position, if it is a negative number, add 1 in the upper position. For example, 3> 1 = 1,-3> 1 =-2. The binary encoding of 3 is 0000 0011. Move it to the right by 1 bit to get 0000 0001. convert it to 1 in decimal format, and the binary encoding of-3 is 1111 1101, move it to the right to get 1111 1110. This is a negative number. The negative number is converted to decimal. First, the value is reduced to 1111 1101, And the inverse value is 0000. Then, the value is-2.

The shifted right operator divides the decimal number by the power of two digits and discards the remainder.

Unsigned right shift operator (>>>)

The result of the unsigned right shift operation of a positive number is the same as that of the signed right shift operation, mainly the unsigned right shift operation of a negative number. The difference between it and the shift to the right of the symbol is that, whether it is positive or negative, the high position is supplemented with 0. Therefore, for a positive number, the operation of the symbol and the unsigned operation is the same, negative numbers are a world of difference. For example:-1> & gt1 = 2147483647, the number is terrible, right? Look at the computing process: the binary code of-1 is 1111 1111 1111 1111 1111 1111 1111 1111, shift it to the right and add 0 to get 0111 1111 1111 1111 1111 1111 1111 1111 230. the first digit is 0, which is a positive number. convert it to decimal format: 229 + + ...... + 20 = 230 (1-1/231)/(1-1/2) = 231-1 = 2147483647. In this way, we finally get the expected results. The results are terrible and use them with caution!

Application of bitwise operators:

After talking for so long, the ultimate goal is to use these operators. Let's look at some examples:

Conversion of RGB values and hexadecimal values of colors: for example, a color value: #33cc10, the first two represent red (R), and the middle two represent green (G ), the last two digits represent blue (B) and convert them to binary encoding: 0011 0011 1100 1100 0001 0000 (assigned to color). First, we need to get the red value, we need to shift it to 16 bits, color> 16, that is, 0000 0000 0000 0000 0011 0011. In this way, we get R = 51, to obtain the green value, we need to first shift it to eight places, color> 8, and get 0000 0000 0011 0011 1100, then change the first eight bits to 0000 0011 0011 1100 1100 0000 | 0000 0000 0000 1111 1111 0000, get 0000 0000 0000 1100 1100 204, so that we can get G =, and finally get the blue value, is to simply change the first eight digits to 0, color | 0 000 0000 0000 0000 0001 0000, we get B = 16, #33cc10 is converted to RGB value (51,204, 16 ). In turn, converting RGB to hexadecimal is exactly the opposite method, that is, G <16 | G <8 | B, which is not described here.

Determine whether a node is the parent node of another node. For example, if two nodes a and B exist, the ie Method is. contains (B) is used to determine whether a is a child node of B, while other modern browsers use. compareDocumentPosition (B). The returned result is not a boolean value. If a and B are the same node, 0 is returned, if a and B are in different documents or at least one is out of document, 1 is returned. If B is before a, 2 is returned. If a is before B, 4 is returned, if B contains a, 8 is returned. If a contains B, 16 is returned, and 32 is exclusive to the browser. The binary codes of 0, 1, 2, 4, 8, and 16 are 0000 0000, 0000, 0001, 0000, 0010, 0000, 0100, and 0000, respectively. compareDocumentPosition (B) & 16 is converted to true or false to judge whether a is a node of B. Why not use. compareDocumentPosition (B) = 16? Because. compareDocumentPosition (B) returns 20 (4 + 16), so you can use. compareDocumentPosition (B) = 20. The advantage of the & operator is that we do not need to consider this, we only need to consider whether it and the value 16 we need can return true. (John Resig has a method to simulate compareDocumentPosition so that it can be used in ie. If you are interested, refer to the link at the end of this Article ~)

Bitwise left shift: We know that bitwise shifts 1 bit left by multiplying 2, so we can use a <1 to replace a * 2, because bitwise operations are more efficient than normal operations (sometimes the opposite result may be obtained, the speed of the JavaScript median operation is very poor, and the difference from C # is too far ~).

Shift right by bit: on the one hand, you can use a> 1 to replace a/2, and shift right by bit to easily convert decimal points into integers, such as 3.1415> 0 = 3, because the number of operations by displacement must be an integer (For details, refer to the ECMA-262 Manual), so the operation will discard decimal places ~

Note: bitwise operators require that the number of numeric operations be integer, and these operations are represented by 32-bit integers, and 32nd-bit operators are signed. In addition, the number of operations must be within the 32-bit integer range, and the number of operations on the right must be between 0 and 31. (The binary code in this article is not standard, so it is only convenient to use ~)

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.