Relive integers
There are two types of ECMAScript integers, signed integers (which allow positive and negative numbers), and unsigned integers (only positive numbers are allowed). In ECMAScript, all integer literals are signed integers by default.
Signed integers with values ranging from 2147483648 to 2147483647, unsigned integers, and values ranging from 0 to 4294967295. Remember that all integer literals are stored by default as signed integers, and only the ECMAScript bitwise operators can create unsigned integers.
Converts an unsigned integer to a string, returning only the valid bits. That is, 0 of the front is not returned.
var i = +, j = -18;alert (i.ToString (2)); ' 10010 ' Alert (j.tostring (2)); '-10010 '
Although the binary display of negative numbers adds a minus sign before the binary representation of a positive number, negative storage is also binary code, in the form of twos complement.
There are 3 steps in calculating the twos complement step:
- Determines the binary representation of a non-negative version of a number. For example, 18 of the complement must first determine the binary representation of 18
- The binary inverse code is obtained, that is, replacing 0 with 1, replacing 1 with 0
- Add 1 to the binary counter code
Bitwise operator NOT (~)
The bitwise operator is represented by a negation good (~), which is one of the few operators in ECMAScript that are related to binary arithmetic. The bitwise operator NOT is a three-step process:
- Convert the operand to a 32-bit binary number
- and turn it into its binary back code.
- Convert binary inverse code to floating point
Feeling very troublesome, and the above operation simply, can be equivalent to the number of negative, and then minus 1, such as~25 === -26
Bitwise operators and (&)
It operates on the binary of numbers, converts the numbers to binary, aligns the digits in each number, and then carries out and operations with the following rules:
1 to 1-1
1 to 0-0
0 to 1-0
0 to 0-0
So25 & 3 = 1
Bitwise operator OR (|)
The same as operator, except that its rules are:
1 to 1-1
1 to 0-1
0 to 1-1
0 to 0-0
So25 | 3 = 27
Bitwise operator XOR (^)
Ditto, except that the rules are:
1 to 1-0
1 to 0-1
0 to 1-1
0 to 0-0
So25 ^ 3 = 26
Left shift operation («)
Shift operations are generally not used in development, and are used in extremely optimized code such as drivers. A left-shift operation shifts all digits of a number to the left by the specified number. When you move the digits to the left, the extra empty space on the right side of the number is filled by 0, making the result a full 32-digit number. Note: A left-shift operation retains the sign bit of a number. "is the symbol still stored in 32nd place?" Yes
Signed right Shift operation (»)
It shifts all the digits in the 32-digit number to the right, while preserving the symbol for that number. The vacancy is created after the shift, where the vacancy is on the left side of the number, on the right side of the symbol, and ECMAScript fills the vacancy with a sign bit to create a complete number.
Unsigned Right shift operation (»>)
It shifts all the digits in the 32-digit number to the right. For positive numbers, it is the same as a signed right-shift operation. For negative numbers, the situation is different. Unsigned right diverted 0 fills all vacancies. For positive numbers, this is exactly the same as a signed right-shift operation, and a negative number is treated 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 the negative numbers results in a very large digit. To achieve this, you need to convert the number to an unsigned equivalent (although the number is still signed), which can be converted:
var iUnsigned64 = -64 >>> 0;
It then uses the number type ToString () to get its true bit representation, using a base of 2:
Alert (iunsigned64.tostring (2));
The
result generates 11111111111111111111111111000000, which is the binary complement of the signed-64 representation, but it equals the unsigned integer 4294967232. For this reason, be careful when using the unsigned right-shift operator.