JavaScript binary operation techniques parsing _javascript techniques

Source: Internet
Author: User
Tags bitwise numeric value php error pow
1, the original code, the inverse code, the complement, the positive subtraction to complement addition
JS in the binary operation, the use of 32-bit binary integer, because the whole number of JS is signed, the highest bit 0 for positive numbers, 1 for negative numbers, therefore, the JS binary operation used in the integer expression range is
Copy Code code as follows:

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

Original code: The highest bit 0 indicates positive, 1 is negative, the remaining 31 is the absolute value of the number (absolute value of the truth) in the binary form
Anti-code: Positive inverse code is the same as the original code, negative inverse code is the original symbol bit unchanged, the remaining 31-digit reverse (0 1,1 to 0)
Complement: The same number of positive and original code, negative complement for the inverse code plus 1 (symbol bit to participate in the operation, in fact, only 0 of the complement to involve the highest carry, so do not worry about in the counter code plus 1 o'clock because the symbol bit to participate in the operation of the decimal-variable +)
+0 of the counter code: 32 0, by positive processing, the original code, back code, complement is 0.
-0 of the counter code: the highest bit 1, the remaining bits from +0 original code to take the counter, get 32 1
-0 of the complement: its inverse code is 32 1 plus 1, the highest bit overflow is discarded, get 32 0
Therefore, the complement of Plus and minus 0 is 0.
By the complement of negative numbers, he is asked for absolute value: the absolute value of negative binary number, as long as everyone (including sign bit) to reverse, plus 1, we get its absolute value.
When the computer is processing the addition and subtraction operation, uses the complement to carry on the operation, the subtraction is considered to add a negative number, when processing negative numbers, uses the complement of the negative number to be able to obtain the correct operation result, the complement is in order to unify adds the subtraction operation to be born.
The principle of positive subtraction and complement addition is 32-digit overflow:
For a 32-bit binary positive integer, it is modeled as
Copy Code code as follows:

Math.pow (2,32) = 4294967296

The maximum expression range for a 32-bit positive integer is 4294967296-1, the value of 4294967296 is to carry to 33 bits, 33 bits are discarded, only 32 0 (this is the same as the clock on the dial 0 and 12), the dial is modeled by 12), As a result, a number increases gradually, once the number of M exceeds 4294967296-1 can be expressed as m%4294967296
Negative-M (the absolute value of M) can be represented as a positive number: 4294967296-m (this positive number is a negative number of the complement of the binary positive integer, minus the complement of 32-bit binary, and his original code added just equals modulo), the same as the dial, 11 points and minus 1 points in the same position.
Take-3 For example:
Copy Code code as follows:

(Array). Join ("0") + (3). ToString (2)). Slice (-32); |-3| Binary number, that is, the original code
Original code = 00000000000000000000000000000011;
Inverse code = 11111111111111111111111111111100; The original code symbol bit is 1, the remaining bits are reversed
complement = 11111111111111111111111111111101; The inverse code plus 1 because the inverse code by the positive form of the original code low 31-bit, so the lower 31 digits of these two numbers are all 1, plus the counter code sign bit 1, get 32 1
Well, there
Complement + Original code = (inverse code + 1) + Original code
= (Inverse code + original code) +1
= 1+ (32 bits are all 1 binary numbers)//Because the inverse code is obtained by the lower 31 digits of the original code in the positive form and the sign bit 1. So the lower 31 bits of these two numbers are all 1, plus the code sign bit 1, which gets 32 1
= Math.pow (2,32)
= 4294967296//This is the modulo of the 32-bit binary number, just like the |-1|+11 = 12 principle

This is: Positive subtraction-> Negative addition-> complement addition process.
2, bit operation
Because JS integer By default is a signed positive number, so in the operation, only use 31 bits, developers can not access the highest.
Bitwise operations occur only on integers, so a floating-point number is rounded down before it is involved in the bitwise operation.
To avoid accessing the symbol bit, JavaScript converts to the binary of the symbol and its absolute value in the binary of the actual negative number, such as:
Copy Code code as follows:

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

Bitwise REVERSE (~): unary operation, 1 variable 0,0 to 1, such as
~123;//-124.
You can verify this process: Positive numbers reverse, sign bit negative, so the result is a negative number, according to Math.pow (2,32)-M can be represented as-m, you can do the following method calculation
Copy Code code as follows:

parseint ((Array). Join (0) + (123). ToString (2)). Slice ( -32). Replace (/\d/g,function (v) {
Return (v*1+1)%2;
}), 2-math.pow (2,32)//-124, if negative minus MATH.POW (2,32)

It is to be noted that the JavaScript bit operation is signed, so it reaches 32 bits, and its highest bit will be the symbol bit, and the number of the positive (modulo math.pow (2,32) complement-two numbers is added to the modulo, and the two numbers complement each other).
The inverse of the integer M bitwise can be calculated as follows:
Copy Code code as follows:

(( -1*m-1) +math.pow (2,32))%math.pow (2,32)

Bitwise AND (&): Two digits of the same bit, are 1 return 1, otherwise return 0
Copy Code code as follows:

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

Copy Code code as follows:

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

Bitwise XOR OR (^): Two number of the same bit, one is 1 the other is 0 then return 1, otherwise return 0
Copy Code code as follows:

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

Some characteristics of an XOR operation:
Copy Code code 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 can be launched a = d ^ b ^ C//This feature is used in some cryptographic algorithms
If the operand is within the appropriate range (not overflow), such as a=1,b=2, swap the value of two variables
A = [b,b=a][0]
Or
A = A^b
b = A^b
A= a^b

Use a bitwise XOR to record multiple messages using a number:
Several status values are 1, 2, 8, 16 ...
The rule of these values is that only one of their binaries is 1, and the rest are 0, therefore, any number of them in the bitwise XOR or the result of the operation will not appear two number of a 1 of the case, and the value of the operation is only determined, that is, know the result of the operation, you know which number of combinations, This allows you to record multiple messages with a single digit.
Copy Code code as follows:

00000001
00000010
00000100
00001000
00010000

1^2^4 = 7//"00000111"
Therefore, if we know that the result is 7, we know that they are composed of 1, 2 and 4.
If we want to set a parameter that contains several state values, we can use the bitwise OR operation
Examples of this can refer to several constants in PHP about the type of picture, and several constants defined by the PHP error level.
Such examples, perhaps useful decimal numbers to describe, such as: single-digit digits indicate the state of a property, 10-digit number represents the state of another property, so that each state can have 10 values, only one number can describe a combination of very many.
Left Shift (<<): A number of binary all bits to the left, symbol bit fixed, high overflow discard, low fill 0
If not overflow, the effect of the left shift is multiplied by 2.
Right Shift (>>): A number of binary all bits move to the right, symbol bit fixed, high up 0, low drop
The effect of the right shift operation is divided by 2 and rounding down.
Sign right Shift (>>>): The sign bit follows the move when the shift is carried, the sign bit is also treated as a numeric value, so the result of the operation is a 32-bit unsigned integer, so the signed right shift of a negative number produces a positive integer, and the signed right of the positive is the same as the unsigned right shift, which is the only operation that can manipulate the sign bit.
-123>>>1;//2147483586.
some place to look out for.
The bitwise operation must be an integer, and if the operand is not an available integer, 0 will be taken as an operand
Copy Code code as follows:

~nan; ~0 will be executed with a result of-1
~ ' x '; -1
' Hello ' |; 0
({}) |; 0

Displacement operation can not move more than 31 digits, if you attempt to move more than 31 digits, the number of digits to 32 after modulo shift
123&GT;&GT;32//actual is 123>>0 (32%32 = 0)
123&GT;&GT;33//actual is 123>>1
32-bit signed integer expression range is-math.pow (2,31) ~ Math.pow (2,31)-1 is -2147483648~2147483647, while the accuracy of JS numbers is double precision, 64 bits, if a more than 2147483647 Integer participation in the bitwise operation when it is necessary to note that the second system overflow, intercept 32 digits, if the 32nd digit is 1 will be interpreted as negative (complement).
Copy Code code as follows:

>>0; -2147483648
>>0; 0
>>0; -1
>>0; 1

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.