Converts a binary number to a floating point number.For example:
Var iNum1 = 25; // 25 equals 00000000000000000000000000011001var iNum2 = ~iNum1;
// Convert it to 11111111111111111111111111100110 alert (iNum2); // output "-26"
The bitwise operation NOT is to calculate the negative value for the number and then subtract 1, so 25 is changed to 26. The following method can also be used to obtain the same method:
Var iNum1 = 25; var iNum2 =-iNum1-1; alert (iNum2); // output-26
AND (&)AND operations AND are represented by AND signs. It alignment the digits in each number, AND then use the following rules to perform the AND operation on the two digits in the same position (if AND only if the two digits are 1, 1 is returned ):
Number in the first digit |
Number in the second digit |
Result |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
For example, to perform AND operations on Numbers 25 AND 3, the Code is as follows:
Var iResult = 25 & 3; alert (iResult); // output "1"
The result of the AND operation on 25 AND 3 is 1. Why? The analysis is as follows:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------AND = 0000 0000 0000 0000 0000 0000 0000 0001
It can be seen that in 25 and 3, only one digit (bit 0) Stores 1, so other digits generate 0, so the result is 1.
OR (|)OR is expressed by the symbol (|. When calculating each digit, the OR operator follows the following rules (0 is returned only when both digits are 0 ):
Number in the first digit |
Number in the second digit |
Result |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
Use the AND operator as an example to perform OR operations on 25 AND 3. The Code is as follows:
Var iResult = 25 | 3; alert (iResult); // output "27"
The result of performing the OR operation on 25 and 3 is 27:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011--------------------------------------------OR = 0000 0000 0000 0000 0000 0000 0001 1011
It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11011 equals 27.
XOR (^)The XOR operation is represented by a symbol (^. XOR is different from OR. When only one digit stores 1, it returns 1. The truth table is as follows:
Number in the first digit |
Number in the second digit |
Result |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
Perform XOR operations on 25 and 3. The Code is as follows:
Var iResult = 25 ^ 3; alert (iResult); // output "26"
The result of performing XOR operations on 25 and 3 is 26:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------XOR = 0000 0000 0000 0000 0000 0000 0001 1010
It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11010 equals 26.
Left shift operation (<)The Left shift operation is represented by two smaller signs (<). It moves all digits in a number to the left. For example, if the number 2 (equal to 10 in binary) is shifted to 5 digits, the result is 64 (equal to 1000000 in binary ):
Var iOld = 2; // equals to binary 10var iNew = iOld <5; // equals to binary 1000000 decimal 64
Note:Five more spaces are available on the right of the number when the number is shifted to the left. The Left shift operation fills these spaces with 0, making the result a complete 32-bit number.
Note:The Left shift operation retains the sign bit of the number. For example, if you move-2 to the left to 5 digits, the result is-64 instead of 64. "Is the symbol still stored in the 32nd-bit format ?" Yes, but this is done in the ECMAScript background. Developers cannot directly access 32nd digits. Even if a negative value in the form of a binary string is output, it is displayed as a negative number (for example,-2 will display-10 .)
Right Shift operation (>)Bitwise operationThe shifted right operator is represented by two greater signs (> ). It shifts all the digits in the 32-bit number to the right and retains the symbol (positive or negative) of the number ). The bitwise shift operator is the opposite of the bitwise shift operator. For example, to shift 64 to the right, change to 2:
Var iOld = 64; // equals to binary bytes 00var iNew = iOld> 5; // equals to binary 10 decimal 2
Similarly, after the number is moved, the space is vacant. This time, the blank space is located on the left side of the number, but behind the symbol bit. ECMAScript fills these spaces with the symbol bit values and creates a complete number, as shown in:
Unsigned right shift operationThe unsigned right shift operator is represented by three greater than signs (>>>). It shifts all digits of the unsigned 32-digit number to the right. For positive numbers, the result of the unsigned right shift operation is the same as that of the signed right shift operation.
In the example of a signed right shift operation, change 64 to 5 bits to 2:
Var iOld = 64; // equals to binary bytes 00var iNew = iOld >>> 5; // equals to binary 10 decimal 2
For negative numbers, the situation is different.
The unsigned right shift operation fills all spaces with 0. For positive numbers, this is the same as the operation for the signed right shift operation, while the negative number is processed as a positive number.
Because the result of the unsigned right shift operation is a 32-bit positive number, the unsigned right shift operation of negative numbers always produces a very large number. For example, if you shift-64 to the right by 5 digits, 134217726 is returned. How can we get this result?
To achieve this, you need to convert the number into an equivalent form without a symbol (although the number itself is still signed). You can obtain this form using the following code:
var iUnsigned64 = -64 >>> 0;
Then, use the toString () of the Number type to obtain its true bit representation. The base is 2:
alert(iUnsigned64.toString(2));
This will generate 11111111111111111111111111000000, that is, the binary complement representation of the signed integer-64, but it is equal to the unsigned integer 4294967232.
For this reason, be careful when using the unsigned right shift operator.
There are two types of logical operations: one is changing the target data type, and the other is not. A typical example of the former is a non-logical operation. No matter what type of operation element, after a non-logical operation is executed, it is converted to a bool value. For example:var a = "123";var b = !a; // false
The other form does not change the target type and supports Boolean short-circuit operations, including "logical and" (&) and "logical or" (|) operations. The so-called Boolean short circuit refers to determining the operation result when determining the first operation element, without the need to process the second operation element. The specific operation rules are as follows: when the first operation element is true, the logic or operation returns the first operation element (note that the type is not changed ), when the first element of the second element is false, the logic or operation returns the second element, here are some special examples of the first operation element returned by the logic and operation:function and(a, b) { return a && b;}function or(a, b) { return a || b;}var a = and("str", false); // a === falsevar b = or("str", false); // b === "str"var c = and(0, "str2"); // c === 0var d = or(0, "str2"); // d === "str2"
The most common use of this operation is to process real parameters. We know that the real parameter list and the form parameter list in JavaScript can be of varying length. How can we deal with the unequal part? For example:function add(a, b) { return a + b;}var a = add(5); // NaN
The result is NaN, which is obviously not the result we want. We prefer to return 5. We can use logical operations:function add(a, b) { b = b || 0; return a + b;}var a = add(5); // 5var b = add(5, 2); // 7
If the length of the parameter is greater than the length of the real parameter, the excess part is undefined. B = B | 0 can assign 0 to B when B is undefined (Boolean value is false), so that B is still a numerical value and involved in the operation. Of course, this process is still very simple. For example, when the input B is a non-null string, this function will still fail, so the correct method should be to determine whether it is a numerical value type, however, it is a good way to perform logical operations without the exception of API users.
In string operation JavaScript, there is only one string operation, that is, string connection. Of course, strings can also be involved in other operations, such as comparison, equivalence, value assignment, and so on, but we will put them in other categories for discussion. The built-in methods in the string type, such as cutting strings, do not belong to the expression operation described here. The operator number corresponding to the String concatenation operation is "+ ". Note that "+" is a multi-dimensional symbol. It can be used for addition of numeric values, unary operators, and string connections. The specific operation is determined by the number and type of elements. If there are two or more operators and one of the first two operators is a string, other non-strings are converted to strings, and the target type is strings. For example:var a = "str" + "ing"; // a === "string"var b = "str" + 0; // b === "str0"var c = 0 + "str"; // c === "0str"var d = "str" + undefined; // d === "strundefined"var e = null + "str" + undefined; // e === "nullstrundefined"var f = null + undefined + "str"; // f == NaN
The purpose of the comparison calculation equivalence check is to determine whether two variables are the same or equal. The same and different operators refer to the operators "=" and "! = ", Indicating equal and not equal to the operator" = "and"! =. We can use a table to represent:
Equality detection in comparison operations
Name |
Operator |
Description |
Equal |
= |
Compare two expressions to see if they are equal |
Not Supported |
! = |
Compare two expressions to see if they are not equal |
Strictly equal |
=== |
Compare two expressions to see if the values are equal and have the same data type |
Not strictly equal |
! = |
Compare two expressions to see if they have different values or data types. |
Equality calculation rules in equality Detection
Equality calculation rules in equality Detection
Type |
Operation Rules |
Compare two value types |
Convert to values of the same data type for data equivalent comparison |
Comparison between value types and reference types |
Convert the data of the reference type to the value type, and then compare the data equivalent. |
Comparison of Two reference types |
Compare referenced addresses |
If you are vague about the value type and reference type, you can see the previous article about JavaScript variables. Same operation rule in equality Detection
Same operation rule in equality Detection
Type |
Operation Rules |
Compare two value types |
Different data types are bound to be different. Perform numerical equivalent comparison when data types are the same |
Comparison between value types and reference types |
Must be different |
Comparison of Two reference types |
Compare referenced addresses |
We can see that there is no difference between the two reference types, which are the same and equal. Let's look at an example:var str1 = new String("str");var str2 = new String("str");console.log(str1 == str2); // falseconsole.log(str1 === str2); // false
The concept of sequence Detection is very difficult to understand. In other words, it is actually relatively large. There are four comparison operators:>, >=, <, and <=. The meaning is not to be understood by everyone. Three data types are available for sequence Detection: boolean, string, and number. Their sequence values are shown in the following table:
Types of comparable Sequences |
Sequence Value |
Boolean |
0 ~ 1 |
String |
0 ~ 255(* Note 1) |
Number |
NEGATIVE_INFINITY ~ POSITIVE_INFINITY(* Note 2) |
* Note 1: In general languages, character (char) data types are sorted by ASCII code. JavaScript does not contain any character type, but each character in a string is used as a single character for sequence Detection. * Note 2: negative infinity to positive infinity. In fact, there is a boundary, but this boundary has reached 10 ^ 308 (Number can be used. MAX_VALUE. There is no sequence value for the value NaN, and any value (including NaN itself) and NaN will be detected in sequence. The operation rules for the false sequence detection are as follows:
Sequence detection operation rules
Type |
Operation Rules |
Compare two value types |
Directly compare the data size in the sequence |
Comparison between value types and reference types |
Convert the data of the reference type to the value type, and then compare the data size in the sequence |
Comparison of Two reference types |
Meaningless, always returns false |
This part of function call is described in detail in the JavaScript function in the previous section. The operator "()" can only be used after a function or a variable pointing to a function. Otherwise, an exception is triggered.
Special operators have some operators in JavaScript, which are used to affect the operation effect instead of directly generating the operation effect. The operation objects of these operators are usually expressions ", rather than the value of an expression ". In addition, some operators do not directly perform variable value operations, but instead perform variable operations. Detailed operators and their functions are as follows:
Target |
Operator |
Function |
Remarks |
Operation Element |
Typeof |
Returns a string of the data type. |
|
Operation Element |
Instanceof |
Returns an inheritance relationship. |
|
Operation Element |
In |
Returns a member relationship. |
|
Operation Element |
Delete |
Delete a member |
|
Expression |
Void |
Avoid expression return values |
Make the expression always return undefined |
Expression |
? : |
Execute one of the two expressions by condition |
It is also called a three-object conditional operator. |
Expression |
() |
Grouping expressions and adjusting operation order |
Also known as priority Operators |
Expression |
, |
Expressions are executed sequentially. |
Also known as the multiple evaluate or comma Operator |
Computing priority plays an important role in complex operations. For example, we can look at an expression:void 1+2
We know that void is used to make the expression always return undefined. So, is the void operation object 1 or 1 + 2? In other words, which of the following has a higher priority for void and +? Assume that if the void has a higher priority, the number of void operations will be 1, and the operation result will be (void 1) + 2, that is, undefined + 2, the result is NaN. On the contrary, if + has a higher priority, the result will be void (1 + 2), that is, void 3, that is, undefined. So what is it? The fact is NaN, that is, void has a higher priority. We will detail common operators in JavaScript and their precedence. Note that the table has a lower priority from top to bottom.
Priority of operators in JavaScript
Priority |
Operator |
Description |
Highest |
. [] () |
Object member access array subscript function call |
Slave |
++ -~ ~ Delete new typeof void |
Unary operator |
Upper |
*/% |
Modulo of multiplication and division |
To |
+-+ |
Addition and subtraction string connections |
Lower |
<>>>> |
Shift |
Of |
<<=>> = Instanceof |
Sequence Detection inheritance relationship |
Excellent |
=! ===! = |
Equality Detection |
First |
& |
Bitwise AND |
Level |
^ |
Bitwise OR |
Yes |
| |
By bit or |
Yue |
&& |
Logic and |
Come |
| |
Logic or |
Yue |
? : |
Ternary Condition |
Low |
= Op = |
Value assignment |
Lowest |
, |
Multiple evaluate |
When different or identical operators with the same priority appear in a statement at the same time, they are executed from left to right. Of course, like in CSS! Important and JavaScript also have a symbol that forcibly changes the priority, that is, (). The operation in parentheses takes precedence over the operation outside brackets.