Home >Web Front-end >JS Tutorial >JavaScript binary operation skills analysis_javascript skills

JavaScript binary operation skills analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:47:581435browse
1. Original code, complement code, complement code, positive subtraction to complement addition
js uses 32-bit binary integers when performing binary operations. Since the integers of js are all signed numbers, The highest bit 0 represents a positive number and 1 represents a negative number. Therefore, the integer expression range used in js binary operations is
Copy code Code As follows:

-Math.pow(2,31) ~ Math.pow(2,31)-1 // -2147483648 ~ 2147483647

Original code: highest Bit 0 represents positive, 1 represents negative, and the remaining 31 bits are the binary form of the absolute value of the number (the absolute value of the true value)
one's complement: the one's complement of positive numbers is the same as the original code, and the one's complement of negative numbers is the symbol of the original code bit remains unchanged, and the remaining 31 bits are inverted (0 changes to 1, 1 changes to 0)
Complement code: the complement code of a positive number is the same as the original code, and the complement code of a negative number is the complement code plus 1 (the sign bit participates in the operation, in fact, it only requires Only the complement of -0 involves the carry of the highest bit, so there is no need to worry about - changing due to the sign bit participating in the carry operation when adding 1 to the complement).
The complement of 0: 32 0s, processed as positive numbers, the original code, complement and complement are all 0. The complement code of
-0: the highest bit is 1, and the remaining bits are inverted from the original code of 0, resulting in 32 1s. The complement code of
-0: the complement code is 32 1s plus 1, and the highest bit overflow is discarded. , get 32 ​​0s
Therefore, the complement of positive and negative 0 is 0.
Find the complement of the absolute value of a negative number from its complement: the absolute value of a negative binary number, as long as each bit (including the sign bit) Negate it and add 1 to get its absolute value.
When the computer processes addition and subtraction operations, it uses the complement code to perform operations. Subtraction is regarded as adding a negative number. When processing negative numbers, the correct operation result can be obtained by adding the complement code of the negative number. The complement code is for Born to unify addition and subtraction operations.
The principle of converting positive subtraction to complement addition is 32-bit overflow:
For a 32-bit binary positive integer, its modulus is
Copy code The code is as follows:

Math.pow(2,32) = 4294967296

The maximum expression range of 32-bit positive integers is 4294967296 - 1. When reaching 4294967296, this value must be carried to the 33rd bit. The 33rd bit is the overflow bit and is discarded, only 32 0s are obtained (this principle is the same as the hour hand pointers at 0 o'clock and 12 o'clock on the dial are at the same position. The dial starts with 12 is the modulus), therefore, a number gradually increases, once the number M exceeds 4294967296-1, it can be expressed as MB94967296
and the negative number -M (M is the absolute value) can be expressed as a positive number: 4294967296 - M ( This positive number is the binary positive integer corresponding to the complement of the negative number. The complement of the negative number is a 32-bit binary number, and the sum of its original code is exactly equal to modulo). The principle is the same as the dial. 11 o'clock and negative 1 o'clock point to the same point. a location.
Take -3 as an example:
Copy the code The code is as follows:

(Array( 32).join("0") (3).toString(2)).slice(-32); // The binary number of |-3|, that is, the original code
original code = 00000000000000000000000000000011;
reverse Code = 111111111111111111111111111111100; //The sign bit of the original code is 1, and the remaining bits are inverted
Complement code = 111111111111111111111111111111101; //The inverse code adds 1 because the inverse code is obtained by inverting the lower 31 bits of the original code in positive form, so this The lower 31 bits of the two numbers are all 1, plus the complement sign bit 1, we get 32 ​​1s
Then, there is
complement original code = (complement 1) original code
= (reverse Code original code) 1
= 1 (32 bits are all binary numbers of 1) //Because the complement code is obtained by inverting the lower 31 bits of the original code in positive form and adding the sign bit 1, so these two numbers The lower 31 bits of the sum are all 1, plus the one's complement sign bit 1, we get 32 ​​1's
= Math.pow(2,32)
= 4294967296 //This is exactly the modulus of the 32-bit binary number , the same principle as |-1| 11 = 12

This is: positive number subtraction-> negative number addition-> process of complement addition.
2. Bit operations
Because the integers of js are signed positive numbers by default, only 31 bits can be used in the operation, and developers cannot access the highest bit.
Bitwise operations only occur on integers, so a non-floating point number will be rounded down before participating in bitwise operations.
In order to avoid accessing the sign bit, javascript converts the binary of the negative number into the binary of the sign and its absolute value, such as:
Copy code The code is as follows:

(-123).toString(2) ;// "-1111011"

Bitwise negation (~): unary operation, 1 changes to 0, 0 changes to 1, such as
~123; //-124
You can verify this process: positive numbers are inverted, and the sign bit is Negative, so the result is a negative number. According to Math.pow(2,32) - M can be expressed as -M and can be calculated as follows
Copy code The code is as follows:

parseInt((Array(32).join(0) (123).toString(2)).slice(-32).replace(/d /g,function(v) {
return (v*1 1)%2;
}),2)-Math.pow(2,32) // -124, if it is a negative number, subtract Math.pow (2,32)

It should be noted that javascript bit operations are all signed, so up to 32 bits, the highest bit will be used as the sign bit, and when negated, a positive number should be obtained (take Modulo Math.pow(2,32)'s complement--the addition of two numbers results in modulo, and the two numbers are said to be complements of each other).
The bitwise inversion of an integer M can be calculated as follows:
Copy the code The code is as follows:

((-1*M-1) Math.pow(2,32))%Math.pow(2,32)

Bitwise AND (&): two numbers are the same bits, all are 1, return 1, otherwise return 0
Copy code The code is as follows:

&234 = 106
"00000000000000000000000001111011"
"00000000000000000000000011101010"
-------------------------------- ------------
"00000000000000000000000001101010"

Copy code The code is as follows:

|234 = 251
"0000000000000000000000001111011"
"00000000000000000000000011101010"
-------------------------- --------------------------
"00000000000000000000000011111011"

Bitwise XOR (^): two If the number has the same digit, if one is 1 and the other is 0, then 1 is returned, otherwise 0 is returned
Copy code The code is as follows:

^234 = 145
"0000000000000000000000001111011"
"00000000000000000000000011101010"
------------------------ -----------------------
"00000000000000000000000010010001"

Some features of the XOR operation:
Copy code The code is as follows:

a ^ a = 0 
a ^ b = b ^ a 
a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c
a ^ b ^ a = b
d = a ^ b ^ c It can be deduced that a = d ^ b ^ c //This feature is used in some encryption algorithms
//If the operands are within the appropriate range (no overflow), such as a=1, b=2, exchange the values ​​of the two variables
a = [b ,b=a][0]
//or
a = a^b
b = a^b
a= a^b

using bits The XOR operation uses one number to record multiple pieces of information:
There are several status values ​​​​1, 2, 8, 16....
The rule of these values ​​​​is that only one of their binary digits is 1 , the rest are all 0. Therefore, the result of the bitwise XOR operation of any of them will not have a situation where a certain bit of the two numbers is 1, and the value of the operation is uniquely determined, and That is, if you know the result of the operation, you will know which combination of numbers it is, so that you can use one number to record multiple pieces of information.
Copy code The code is as follows:

00000001
00000010
00000100
00001000
00010000

1^2^4 = 7 // "00000111"
So, if we know that the result is 7 , we know that they are a combination of 1 , 2 , and 4 .
If we want to set a parameter to contain several status values, we can use bitwise OR operation.
For such an example, you can refer to several constants about image types in PHP and PHP error level definitions Several constants.
Such an example may be described with decimal numbers, for example: a single-digit number represents the status of a certain attribute, and a tens-digit number represents the status of another attribute. In this case, each status can have 10 values, there are many combinations that can be described with just one number.
Left shift (<<): All binary bits of a number are shifted to the left, the sign bit remains unchanged, the high bit overflows and is discarded, and the low bit is filled with 0
If there is no overflow, the effect of left shift is to multiply 2.
Right shift (>>): All binary bits of a number are shifted to the right, the sign bit is unchanged, the high bits are filled with 0, and the low bits are discarded.
The effect of the right shift operation is to divide by 2 and move downward Round up.
Signed right shift (>>>): When shifting, the sign bit follows the movement, and the sign bit is also treated as a numerical value. Therefore, the result of this operation is a 32-bit unsigned integer, so the signed right of a negative number The shift will produce a positive integer. A signed right shift of a positive number is the same as an unsigned right shift. This is the only operation that can operate on the sign bit.
-123>>>1 ;//2147483586
Some things to note:
Bitwise operations must be integers. If the operand is not an available integer, it will be 0 As operand
Copy code The code is as follows:

~NaN; // Will execute ~0 , the result is -1
~'x'; // -1
'hello'|0; // 0
({})|0; //0

The displacement operation cannot move more than 31 bits. If you try to move more than 31 bits, take the number of bits modulo 32 and then shift
123>>32 //The actual value is 123>>0 (322 = 0)
123>>33 //actually 123>>1
The expression range of 32-bit signed integer is -Math.pow(2,31) ~ Math.pow(2,31)-1, which is -2147483648 ~2147483647, and the precision of js numbers is double precision, 64 bits. If an integer exceeding 2147483647 is involved in bit operations, you need to pay attention to its binary overflow. After truncating 32 bits, if the 32nd bit is 1, it will be interpreted as Negative (complement).
Copy code The code is as follows:

>>0; //-2147483648
>>0; //0
>>0; //-1
>>0; //1
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn