This article mainly introduces "& amp;" and "|" in Javascript. It has good reference value. Let's take a look at it with the small series below. I. Preface:
Before starting the article, let's take a look at the following questions:
var num1 = 1 & 0;console.log(num1); // 0var num2 = 'string' & 1;console.log(num2); // 0var num3 = true & 1;console.log(num3); // 1var num4 = undefined | false;console.log(num4); // 0var num5 = undefined | true;console.log(num5); // 1var num6 = 23 & 5;console.log(num6); // 5var num7 = 23 | 5;console.log(num7); // 23
Are all the above questions correct? We have previously summarized "talking about" & "and" | "in javascript," & "and" | "are operators in logical operation expressions. What does "&" or "|" mean? What are the features? Next, let's reveal the secrets one by one.
First, we must know that "&" and "|" are bitwise operators.
Bitwise operators are used to operate values at the most basic level, that is, values expressed in memory. All values in ECMAScript are stored in IEEE-754 64-bit format, but bit operators do not directly manipulate 64-bit values. Instead, convert the 64-bit value to a 32-bit integer, perform the operation, and then convert the result to a 64-bit integer. For developers, the 64-bit storage format is transparent, so the entire process is like a 32-bit integer.
For signed integers, the first 31 digits in 32 are used to represent the integer value. A 32nd-bit numeric Symbol: 0 indicates a positive number, and 1 indicates a negative number. This symbol bit is called a symbol bit. The value of the symbol bit determines the format of other bit values. Where, positive numbers are stored in pure binary format, and each bit in 31 represents the power of 2. The first digit (bit 0) indicates 20, the second digit indicates 21, and so on. The bits that are not used are expressed as 0, which means they are ignored. For example, the binary value 18 indicates 0000 0000 0000 0000 0000 0000 0001 0010, or a more concise 10010. This is five valid BITs, which determine the actual value.
Negative numbers are also stored in binary code, but the format is binary complement. To calculate the binary complement of a value, follow these three steps:
(1) Calculate the binary code of the absolute value of this value (for example, the binary complement code of-18 is required, and the binary code of 18 is obtained first );
(2) Calculate the binary anticode, replace 0 with 1, and replace 1 with 0;
(3) Add 1 to the obtained binary anti-code.
In this way, the binary representation of-18 is obtained, that is, 1111 1111 1111 1111 1111 1111 1110 1110.
...... In ECMAScript, when the bitwise operator is applied to the value, the background will undergo the following conversion process: the 64-bit value is converted to a 32-bit value, and then the bitwise operation is performed, finally, the 32-bit result is converted back to the 64-bit value. In this way, it looks like a 32-bit value operation, just like a binary operation in other languages. However, this conversion process also leads to a serious secondary effect. When bitwise operations are applied to special NaN and Infinity values, both values are treated as 0.
If the bitwise operator is applied to a non-numeric value, the Number () function is used to convert the value to a numeric value (automatically completed), and then the bitwise operation is applied. The result is a numerical value. ... (From Javascript advanced programming)
Ii. "&" (by bit AND ):
The bitwise AND operator is represented by an ampersand (&). It has two operators. In essence, bitwise AND operation are to align each of the two values AND perform the AND operation on the two numbers at the same position.
Bitwise AND operation rule: 1 is returned only when the corresponding bits of both values are 1. If any one of the bits is 0, the result is 0.
There are too many theoretical things mentioned above, but I think the theory is necessary. Next, let's analyze the example directly!
Let's take a look at num1, num2, num3, and num6 in the above questions. We try to combine the above theory to analyze why the final result is output.
// Num1 is the return value after the bitwise and operation of 1 and 0. The binary code of 1 is abbreviated as. the binary code of 0 is abbreviated as 0. According to the above rule, the second operator is 0 and the result is 0var num1 = 1 & 0; console. log (num1); // 0 // The First operator Number is a string. According to the theory in the preface, the Number () function is used to process non-numeric operators first, naN is returned and NaN is treated as 0. Therefore, the final result is 0var num2 = 'string' & 1; console. log (num2); // 0 // true is a Boolean value, which is also processed using the Number () function. After processing, the value 1 is obtained, the expression is equivalent to "1 & 1" for bitwise operation. When both values are 1, 1var num3 = true & 1; console is returned. log (num3); // 1 // the binary code of 23 is :... the binary code of is :... 00101. Then each bit is aligned and combined with the above rules, we can get the result of 10111 & 00101: 00101. 00101 is 5var num6 = 23 & 5; console. log (num6); // 5 // Add an example: the binary code of 24 is..., the binary code of 7 is... 00111. Execute the AND operation on two numbers at the same position. The result is... 00000. So the final result is 0. Are you correct? Var add1 = 24 & 7; console. log (add1); // 0
3. "|" (by bit OR ):
The bitwise OR operator is represented by a vertical sign (|), which also has two operators. In essence, bitwise OR operations also align each of the two values and perform the OR operation on the two numbers at the same position.
Bitwise OR operation rule: if one of the two values is 1, 1 is returned, and 0 is returned only when both digits are 0.
Let's take a look at the above example!
// The First operator is undefined, and the second operator is false, which is not a numerical value. Therefore, the Number () function must be used first, and NaN is returned for all processing results, naN is treated as 0 again, so the final result is 0var num4 = undefined | false; console. log (num4); // 0 // The number of the first operator is equal to 0, and the number of the second operator is equal to 1. Combined with the bitwise OR rule, the final result is 1var num5 = undefined | true; console. log (num5); // 1 // the binary code of 23 is :... the binary code of is :... 00101. Then each bit is aligned and combined with the above rules, the result of 10111 | 00101 is 10111. 10111 is 23var num7 = 23 | 5; console. log (num7); // 23 // Add another example: the binary code of 24 is..., the binary code of 7 is... 00111. Execute the AND operation on two numbers at the same position. The result is... 11111. So the final result is 31. Are you correct? Var add2 = 24 | 7; console. log (add2); // 31
4. Others:
I believe there will also be some friends who don't know how to convert the value into a standard binary code. Is there a quick way? The answer is yes.
I randomly found an online Conversion Tool address on the Internet: Numerical Conversion (Click here to view ). (Of course, you can also use other tools you find. In any case, achieving results is our ultimate goal)
Finally, I will attach the regular chart that I summarized in the binary process by hand to quickly convert the value into a binary code!
For more details about "&" and "|" in Javascript, please follow the PHP Chinese website!