JavaScript basic Concept (iv)---operator

Source: Internet
Author: User
Tags bitwise bitwise operators

Operator 1. Unary operators

Operators that can manipulate only one value are called unary operators, and unary operators have increment (decrement) operators and unary plus (minus) operators.

The increment decrement operator is divided into the forward increment (decrement) operator and the post increment (decrement) operator, whether it is a pre-or post-increment (decrement) operator, which is used in the same way as the increment decrement operator in Java. The difference is that the increment-decrement operator in JavaScript can manipulate values of non-numeric types, as shown in the following example:

A. When you perform an increment-decrement operation on a string that contains a valid numeric character, the string is converted to a number before the increment decrement operation is performed. The string is finally converted to the number type.

B. For a string that cannot be converted to a number, a Nan is returned, and the original string becomes Nan.

C. For Boolean values, True converts the 1,false to 0 to perform an increment-decrement operation.

D. The object type, first executes the object's valueof () method, increments the value taken above, and if it returns Nan, then the ToString () method is incremented in the same manner as above.

The forward increment decrement operator, the value of the variable is changed before the statement is evaluated, the increment decrement operator, the value of the variable is changed after the statement is evaluated.

For unary plus or minus operators, unary plus operation utilises (+) indicates that the unary minus operation is utilises (-). Using unary plus or minus operators for numbers is the same as the addition and subtraction operators in math, but when you use the unary plus minus operator for non-numeric values, the number () function is equivalent to the current value. The specific operation is as follows:

2. Bitwise operators

The bitwise operator is actually manipulating the values in memory to represent the digits of the numeric value. All values in ECMAScript are stored in the IEEE-754 64-bit format, but the bitwise operator does not directly manipulate the 64-bit value, but instead converts the 64-bit value to 32 bits, then performs the operation to convert the result of the operation back to 64 bits.

For integers with a minus sign, the first 31 bits in 32 bits store values, 32 bits are sign bits , 1 are negative numbers, and 0 represents positive numbers. Where positive numbers are stored in a pure binary format, each of the 31 bits is an exponential power of 2, and the 2 index starts at 0 and is backwards and forwards. unused bits are filled with 0 . negative numbers are also stored in binary, but the format used is twos complement (please use your own Baidu for calculation). In ECMAScript, the binary representation of a negative number is the absolute value of the negative number of the binary preceded by a minus sign, for example, 18 of the binary code is 10010, then-18 of the binary code is-10010.

When a bitwise operator is applied to a value in ECMAScript, the first 64-bit value is converted to 32 bits, then the bitwise operation is performed, and the 32-bit result is converted back to 64 bits after the operation is completed. Where nan and infinit apply bit operators, they are treated as 0. For non-numeric values, the number () function is automatically converted to a numeric value before the bitwise operation, resulting in a numerical result .

2.1 Bitwise non-

Use the wavy line (~) to indicate that the bitwise non-operator returns the inverse of the numeric value, as shown in the following example:

You can see that after performing a bitwise non-operation on 25, the result is that the binary code that returns -26,-26 is preceded by a minus sign of 26, which is supposed to be twos complement, but the negative value of the binary code of the integer is displayed in ECMAScript. At the same time, the above example shows that bitwise non-operators are equivalent to negative values of the current value minus one .

2.2. Bitwise AND | bitwise OR | bitwise XOR

The

Bitwise AND operator ( & ) indicates that the operator operates on two operands and compares the values of the corresponding positions of the two values according to the rules in the following table. Returns a numeric value. The comparison rules are as follows (the values at the same position of the two numeric value are 1 result is 1 , other results are 0 ):

Bits of the first value

Bits of the second value

Compare results

1

1

1

1

0

0

0

1

0

0

0

0

Examples are as follows:

       bitwise OR operator use ( | ) , which is also the same for two operand operations, the comparison rule is as follows (the value in the same position of the two numeric values as long as there is a 1 , the result is 1 ):

Bits of the first value

Bits of the second value

Compare results

1

1

1

1

0

1

0

1

1

0

0

0

Examples are as follows:

Bitwise XOR or use (^), the operation of the two operands, the comparison rule ( two values in the same position of the same value, the result is 0, the difference result is 1):

Bits of the first value

Bits of the second value

Compare results

1

1

0

1

0

1

0

1

1

0

0

0

Examples are as follows:

2.3. Move left

Left shift Use (<<) represents. As shown below:

As can be seen from the above example, the left shift operator moves the position of the value to the left by the specified number of digits, that is, 5 bits to the left, five positions to the right of the original value after the move, and a null to replenish the empty space. Note: the left shift operator does not affect the sign bit of the operand , that is, 11 moves the 5-bit result to the left is-352.

2.4. Signed Right Shift | Unsigned right Shift

The signed right diverted (>>) says, first look at the example:

A signed right shift moves the value to the right by the specified number of digits, but retains the sign bit, meaning that the right shift of the symbol does not cause the numeric symbol to change, and the left-hand space is padded with the value of the sign bit to get a full value.

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

0

0

0

0

0

The value 128 is converted to a 32-bit binary code as shown above, with the arrow pointing to the sign bit

After you move the 5-bit right, the result is 4, and the binary code looks like this:

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

0

The unsigned right diverted (>>>) indicates that the operator moves all 32 bits of the value to the right, and for positive numbers, the unsigned right shift and the signed right shift result are the same . But for negative numbers, the result is a change, the main reason is two points: first, the unsigned right shift is filled with zeros, the sign right shift is the value of the sign bit to fill the empty space; second, the unsigned right shift will use negative binary codes as positive binary codes, and Since the binary codes of negative numbers exist in the form of the binary code complement of their absolute value, the result of unsigned right shift is very large.

The examples are as follows:

As shown above,-64 binary code is 64 of the binary code complement, that is, 1111111111111111111111111000000,

An unsigned right-shift operation will take this value as a positive binary code, then move the right 5 bits to form 00000111111111111111111111111110, and convert the decimal number to 134217726.

3. Boolean operator 3.1. Logical non-

Logical non-operational trailing characters (! ) indicates that the operator can be used for any value in ECMAScript and returns a Boolean value that, when used on a value, converts the current value to a Boolean value before it is reversed. The specific conversion rules are as follows:

I. The operand is an object and returns false;

II. The operand is an empty string and returns true;

Iii. operand is a non-empty string and returns false;

The Iv. operator is the numeric value 0, which returns true;

VI. The operand is a non-0 value (including infinity) and returns false;

VII. The operand is null and returns true;

Viii. the operand is Nan and returns true;

IX. The operand is undefined and returns true;

3.2. Logic and

The logical AND operator use (&&) notation indicates that, as in Java, the result is true if two operands are true, and false if one side is false. However, in JavaScript, the data types of the two operands are not necessarily Boolean or other data types, and the criteria for returning the results are as follows:

I. If the first operand is an object, then the second operand is returned;

II. If the second operand is an object, the object is returned only if the first operand returns true;

Iii. if the two operands are objects, then the second operand is returned;

Iv. If an operand is null, then NULL is returned;

V. If one of the operands is Nan, then return nan;

VI. If an operand is undefined, then return to undefined;

Note: The logic and operation are short-circuiting operations, and the second operand is no longer evaluated as long as the first operand can determine the result. You cannot have an undeclared variable in two operands, or it will result in an error.

3.3. Logical OR

Logical or operational trailing characters (| | ) indicates that one of the two operands is true, so the result is true. Similarly, if one of the two operands is not a Boolean value, the logic or not necessarily returns a Boolean value, as follows:

I. Returns an object if the first operand is an object;

II. If the evaluation result of the first operand is false, then the second operand is returned;

III. If the two operands are objects, the first operand is returned;

Iv. If all two operands are NULL, then NULL is returned;

V. If the two operands are Nan, then the Nan is returned;

VI. If the two operands are undefined, then return to undefined;

4. Multiplicative operators

The ECMAScript specifies three multiplicative operators, namely multiplication, division, and modulo, which are the same as those in Java, and the other is that the automatic type conversion is performed when the operator is not a numeric value.

4.1. Multiplication

Multiplication operation trailing characters (*) indicates that the term calculates the product of two numbers, and when the operand is a special case, the processing rule is as follows:

I. If the two operands are numeric, they can be calculated according to the mathematical multiplication, but return Infinity or-infinity when the result of the calculation exceeds the range of values specified by ECMAScript;

II. If one of the operands is Nan, then return nan;

III. If the infinity is multiplied by 0, the result is Nan;

Iv. If the Infinity is multiplied by a non-zero value, the result is Infinity or-infinity, and the symbol depends on a value other than 0;

V. If the infinity is multiplied with infinity, the result is still infinity;

Vi. If an operand is not a numeric value, then the call to number () function is converted to a numeric value before performing the above operation.

4.2. Division

The Division operation trailing characters (/) indicates that the second operand, in addition to the calculation of the first operand, also has the following special case processing rule:

I. If the two operands are numeric, perform a mathematical calculation and return Infinity or-infinity if the quotient exceeds the range of values specified by ECMAScript.

II. If one of the operands is Nan, then return nan;

III. If the infinity is infinity, the result is Nan;

Iv. if it is 0 divided by 0, the result is Nan;

V. If a non-zero finite number is removed by 0, then the Infinity or-infinity is returned, and the symbol depends on the divisor;

VI. If Infinity is removed by any non-0 finite number, then the return result is Infinity or-infinity.

VII. If an operand is not a numeric value, then the call to number () function is converted to a numeric value before performing the above operation.

4.3. Mould-Finding

The modulo operator is represented by (%), which represents the remainder of the first operand divided by the second operand, and the processing rule is as follows:

I. If the two operands are numeric, perform mathematical operations;

II. If the dividend is infinitely large and the divisor is limited, then return nan;

III. If the dividend is infinitely large, the divisor is 0, then return nan;

Iv. If the infinity is divided by infinity, the result is Nan;

V. If the divisor is a finite number, the divisor is an infinitely large value, and the divisor is returned;

VI. If the divisor is 0, the result is 0;

VII. If an operand is not a numeric value, then the call to number () function is converted to a numeric value before performing the above operation.

5. Additive operator 5.1. addition

The addition operator uses (+) to indicate that if two operands are numeric, perform a regular addition operation, and calculate the result according to the following rules:

I. If there is a number that is Nan, then the result is Nan;

II. If it is infinity plus infinity, the result is infinity;

III. If it is-infinity plus-infinity, the result is-infinity;

Iv. if it is Infinity plus-infinity, the result is Nan;

V. If it is +0 plus-0, the result is +0;

VI. If it is +0 plus +0, the result is +0;

VII. If it is-0 plus-0, the result is-0;

If you have an operator that is a string, you need to use the following rule for a long time:

    1. If the two operands are all strings, then the two strings are stitched together;
    2. If only one operand is a string, then the other operand is converted to a string, and then the two are stitched together;

If an operand is an object, a numeric value, or a Boolean, call their ToString () method to fetch the appropriate string, apply the preceding rule about the string, and if it is undefined or null, call the string () function to get to the corresponding strings " Undefined "and" null ";

5.2. Subtraction

The subtraction operator uses (-) to indicate that the specific operation rules are as follows:

I. If the two operators are numeric, then the mathematical operation can be;

II. If one of the operands is Nan, the result is Nan;

III. If it is infinity minus infinity, the result is Nan;

Iv. if it is-infinity minus-infinity, the result is Nan;

V. If it is Infinity minus-infinity, the result is Infinity;

VI. If it is-infinity minus Infinity, the result is-infinity;

VII. If +0 minus +0, the result is +0;

Viii. if +0 minus-0, the result is-0;

IX. If 0 minus-0, the result is +0;

X. If an operand is a string, Boolean, NULL, or undefined, then the call number () function is converted to a numeric value and then the subtraction calculation is performed based on the preceding rule;

XI. If an operand is an object, the valueof () method of the calling object obtains the value that represents the object, and if the resulting value is Nan, the result of the subtraction is Nan, and if the object does not have a valueof () method, the call to ToString () Method and converts the string that is taken to a number.

6. Relational operators

The ECMAScript uses <,>,<=,>= these relational operators to compare the size of two values, with the following specific rules:

I. If the two operands are numeric, then the mathematical operation can be done;

II. If all two operands are strings, compare the character encoding values of the two strings;

III. If one operand is a numeric value, the other operand is converted to a numeric value, and then a numeric comparison is performed;

Iv. If an operand is an object, call the object's ValueOf () method first, compare it with the resulting value, and, if the object does not have a valueof () method, call the ToString () method of the object and compare it with the resulting value according to the preceding rule.

V. If an operand is a Boolean value, it is first loaded with a value before being compared.

Let's look at two examples:

In this example, the lowercase letter A is compared with the lower case letter B and the capital letter B and finds a less than B but greater than B, because two strings are compared, which is actually a character encoding value that compares each character in the corresponding position in two strings . The character encoding value of the uppercase letters is all less than the character encoded value of the lowercase letter, so a<b returns FALSE.

A second example:

In this example, the first is the string "23" and the string "3" comparison, the result of the comparison is "23" is less than "3", for the same reason as the previous example, two string comparison is the character encoding, "23" character encoding is 50, and "3" character encoding is a, Therefore, the return result is true. For the string "23" and the number 3 comparison, because one operand is a number, the string is converted to a number after comparison, and therefore returns false.

7. Equality operators

The equality operator is divided into two types, the first of which is equality and inequality, and when a conversion is required, the first conversion is then compared, the second is congruent and not congruent, and is not converted at this time and is directly compared.

7.1. Equality and inequality

The equality operation trailing characters (= =) indicates that the unequal operation trailing characters (! =) indicates that both operators will be forced to transform the operands before comparing the two equals or not.

The equality and inequality operators follow the following rules when forcing transformations:

I. If one operand is a Boolean, convert it to a number first, false to 0,true to 1;

II. If one operand is a string and the other operand is a number, then the string must first be converted to a number before being compared;

III. If one operator is an object and the other is not, then the valueof () method of the object needs to be called, and the resulting base type value is compared with the preceding rule;

Iv.null and undefined are equal;

V. Before comparison, null or undefined cannot be converted to another value;

Vi. if one of the operands is Nan, the equality operator returns FALSE, and the inequality operator returns true if the two operands are Nan, and the equality operator returns false;

Vii. if the two operands are objects, then compare whether the two operators point to the same object, and if so, the equality operator returns TRUE, otherwise it returns false;

7.2. Congruent and non-congruent

The only difference between congruent and non-congruent and equal and unequal is that the operand is not converted before comparison, and the congruent operation trailing characters (= = =) indicates that when two operands do not need to be converted to be equal, return true, not congruent (!==) representation.

Note: null = = undefined returns True because both are similar values, but null = = = undefined returns false because both are not of a type, and it is recommended to use both congruent and non-congruent operators.

8. Conditional operator, assignment operator, comma operator 8.1. Conditional operator

The conditional operator is the ternary comparison that we belong to, which means: Boolean_condition? True_value:false_value. Returns True_value when the condition is true, and returns False_value when the condition is false.

8.2. Assignment operators

The function of the assignment operator is to assign the value on the right side of the operator to the left variable, the simplest assignment operator is =, the other operator has *=,/=,%=,+=,-=,<<= (left shift Assignment), >>= (Signed right Shift assignment), >>>= ( Unsigned Right Shift Assignment)

8.3 comma operator

You can use the comma operator to perform multiple operations in a single statement, as follows:

var num1 = 1, num2 = 2, num3 = 3;

The operator section is finally over, so tired.

JavaScript basic Concept (iv)---operator

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.