JavaScript binary Operations

Source: Internet
Author: User
Tags bitwise modulus php error pow

1, the original code, anti-code, complement, positive subtraction of the complement of the addition
JS in the binary operation, the use of 32-bit binary integers, because JS integer are signed number, the highest bit 0 for positive numbers, 1 for negative numbers, so the JS binary operations used in the integer expression range is
Copy CodeThe code is as follows:
-math.pow (2,31) ~ Math.pow (2,31)-1//-2147483648 ~ 2147483647
The original code: the highest bit 0 means positive, 1 is negative, the remaining 31 bits are the binary form of the absolute value of the number (absolute value of truth)
Anti-code: Positive anti-code and the original code is the same, negative anti-code is the original code sign bit unchanged, the remaining 31-bit inversion (0 change to 0)
Complement: The positive complement and the original code is the same, negative complement to the inverse code plus 1 (sign bit to participate in the operation, in fact, only 0 of the complement is involved in the most high-level carry, so do not worry in the anti-code plus 1 o'clock because the sign bit to participate in the arithmetic carry and make-change +).
+0 of the anti-code: 32 0, according to the positive processing, the original code, anti-code, the complement is 0.
-0 of the anti-code: the highest bit 1, the remaining bits are reversed by +0 of the original code, get 32 1
-0 of the complement: its anti-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.
The complement of the negative is the absolute complement of the negative binary number, as long as you (including the sign bit) inverse, plus 1, you get its absolute value.
Computer in the processing of addition and subtraction operations, using the complement operation, subtraction is considered to be added a negative number, in the processing of negative numbers, with the complement of negative numbers can get the correct results, complement is for the unified addition and subtraction operations.
The principle of positive subtraction and complement addition is a 32-digit overflow:
For a 32-bit binary positive integer, its modulus is
Copy CodeThe code is as follows:
Math.pow (2,32) = 4294967296
32-bit positive integer maximum expression range is 4294967296-1, to 4294967296 this value will be rounded to 33 bits, 33 bits is the overflow bit is discarded, only 32 0 (This truth is the dial 0 and 12 points of the hour point is the same position is the same, the dial is 12 modulo), As a result, a number gradually increases, once the number of 4294967296-1 M can be expressed as m%4294967296
Negative-M (m is absolute value) can be expressed as a positive number: 4294967296-m (This positive number is the complement of negative numbers corresponding to the binary positive integer, the complement of negative numbers by 32-bit binary number, and his original code is just equal to modulo), the same as the dial, 11 points and negative 1 points in the same position.
Take-3 For example:
Copy CodeThe code is as follows:
(Array (+). Join ("0") + (3). ToString (2)). Slice (-32); |-3| Binary number, which is the original code
Original code = 00000000000000000000000000000011;
Anti-code = 11111111111111111111111111111100; The original code sign bit is 1, the remaining bits are reversed
complement = 11111111111111111111111111111101; Anti-code plus 1 because the inverse code from the positive form of the original code low 31-bit inversion, so the two number of low 31 bits are all 1, plus the anti-code sign bit 1, get 32 1
Well, there are
Complement + Original code = (inverse code + 1) + Original code
= (Anti-code + original code) +1
= 1+ (32 bits are all 1 binary)//Because the inverse code is obtained by the low 31-bit inversion plus the sign bit 1 of the original code in the positive form, so that the two-digit sum of the low 31 bits is all 1, plus the anti-code sign bit 1, gets 32 1
= Math.pow (2,32)
= 4294967296//This is the modulo of the 32-bit binary number, the same as the |-1|+11 = 12 principle
This is the process of positive subtraction, minus addition, and complement addition.
2, bit arithmetic
Because JS integers are signed by default, so in the calculation, only 31 bits can be used, the developer is not able to access the most high.
Bit operations occur only on integers, so a non-floating-point number is rounded down before it participates in the bit operation.
In order to avoid access to the sign bits, JavaScript is converted to the binary of the symbol and its absolute value when the binary of the actual negative number, such as:
Copy CodeThe code is as follows:
( -123). toString (2);//"-1111011"
Bitwise negation (~): unary operation, 1 change 0,0 to 1, as
~123;//-124
Can verify this process: positive inverse, the sign bit is negative, so the result is a negative, according to Math.pow (2,32)-M can be represented as-m, can be calculated as follows
Copy CodeThe code is 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 should be noted that the JavaScript bit operations are signed, so that the 32-bit, the highest bit will be used as the sign bit, the inverse is due to a positive number (modulus Math.pow (2,32) of the complement-two numbers added to get modulo, said the two number of mutual complement).
The bitwise inverse of an integer M can be calculated as:
Copy CodeThe code is as follows:
(( -1*m-1) +math.pow (2,32))%math.pow (2,32)
Bitwise AND (&): Two digits of the same bit, all 1 returns 1, otherwise 0 is returned
Copy CodeThe code is as follows:
&234 = 106
"00000000000000000000000001111011"
"00000000000000000000000011101010"
---------------------------------------------
"00000000000000000000000001101010"
Copy CodeThe code is as follows:
|234 = 251
"00000000000000000000000001111011"
"00000000000000000000000011101010"
---------------------------------------------
"00000000000000000000000011111011"
Bitwise XOR (^): two digits of the same bit, one is 1 and the other is 0 returns 1, otherwise returns 0
Copy CodeThe code is as follows:
^234 = 145
"00000000000000000000000001111011"
"00000000000000000000000011101010"
---------------------------------------------
"00000000000000000000000010010001"
Some features of XOR operations:
Copy CodeThe 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 can be introduced a = d ^ b ^ C//This feature is used in some cryptographic algorithms
If the operand is within the appropriate range (without overflow), such as a=1,b=2, exchange the value of two variables
A = [b,b=a][0]
Or
A = A^b
b = A^b
A= a^b
Use a bitwise XOR operation to record multiple messages using a number:
There are several status values of 1, 2, 8, 16 .....
The law of these values is that their binary only one is 1, the rest is 0, so any of them in the bitwise XOR or the result of the operation will not appear two number of one is 1, and the value of the operation is the only certainty, that is, know the result of the operation, you know what number of combinations, This allows you to record multiple messages with a single number.
Copy CodeThe code is 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 so that it contains several state values, we can use a bitwise OR operation,
Examples of this can be found in PHP for a few constants about the type of picture, and PHP error levels defined by several constants.
In this case, it might be useful to describe a decimal number, such as a single digit number representing the state of an attribute, a 10-digit number representing the state of another attribute, so that each state can have 10 values, and a number can be used to describe a combination of very many.
Left Shift (<<): A number of binary all bits move left, sign bit fixed, high overflow discard, low fill 0
If it does not overflow, the effect of the left shift is multiplied by 2.
Right Shift (>>): A number of binary all bits move right, sign bit fixed, high 0, low drop
The effect of the right shift operation is divided by 2 and rounded down.
Signed right Shift (>>>): symbol bit follows move when shift, symbol bit is also treated as a numeric value, so the result of this operation is a 32-bit unsigned integer, so the signed right shift of a negative number produces a positive integer, the signed right shift of positive numbers is the same as the unsigned right shift, which is the only operation that can manipulate the symbol bit
-123>>>1;//2147483586
some places to watch out for
The bitwise operation must be an integer, and if the operand is not an available integer, 0 is taken as the operand
Copy CodeThe code is as follows:
~nan; The ~0 will be executed with a result of-1
~ ' x '; -1
' Hello ' | 0
({}) | 0
The displacement operation cannot move more than 31 bits, and if you attempt to move more than 31 bits, the number of bits is modulo 32 and then shifted
123&GT;&GT;32//actual is 123>>0 (32%32 = 0)
123&GT;&GT;33//actual is 123>>1
The 32-bit signed integer expression range is-math.pow (2,31) ~ Math.pow (2,31)-1 that is -2147483648~2147483647, while the accuracy of the JS number is double precision, 64 bits, if a more than 2147483647 Integer participates in bit operations, it is necessary to note that the second binary overflow, after intercepting 32 bits, if the 32nd bit is 1 will be interpreted as negative (complement).
Copy CodeThe code is as follows:
>>0; -2147483648
>>0; 0
>>0; -1
>>0; 1 Reference: Http://www.jb51.net/article/32194.htm bitwise operator: http://www.cnblogs.com/oneword/archive/2009/12/23/1631039.html

1.NOT

The bitwise operator is not represented by ~. The essence of the not operator is to negate the number and then subtract 1.

The bitwise operator NOT is a three-step process.

A. Converting an operator to a 32-digit number

B. Converting binary form to its binary counter code

C. Converting binary inverse codes to floating point numbers

Example:

        var num=10;        document.write (~num);
Results:
-11
2.AND
The bitwise operator and is represented by &. Directly to the binary form of the number. The arithmetic rules are as follows:
First number A second number Results
0 0 0
0 1 0
1 0 0
1 1 1

Example:

        var num1=10;//1010        var num2=11;//1011        document.write (NUM1 & num2);

Results:

Binary representation of 10 1010

3.OR

The bitwise operator or is represented by a symbol |. directly to the binary, the rules are as follows:

First number A second number Results
0 0 0
0 1 1
1 0 1
1 1 1

Example:

        var num1=10;//1010        var num2=11;//1011        document.write (num1 | num2);

Results:

The binary representation of 11 is 1011

4.XOR

The bitwise operator XOR is represented by the symbol ^. Direct binary operation. The rules are as follows:

First number A second number Results
0 0 0
0 1 1
1 0 1
1 1 0

Example:

        var num1=10;//1010        var num2=11;//1011        document.write (num1 ^ num2);

Results:

The binary representation of 1 is 1

5.<<

The left shift operator is represented by <<. It moves all digits in the number to the left by the specified number.

Attention:

A. When you move the digits to the left, the empty space on the right side of the number is filled with zeros so that the result is a full 32-digit number

B. The left shift retains the sign bit of the number.

Example:

        document.write (10<<2+ "<br/>");        document.write ( -10<<2);

Effect:

6.>>

A signed right-shift operation is represented by >>. It shifts all the numbers in the 32-digit number to the right. The symbol for that number is preserved at the same time.

Attention:

A. The sign bit remains the same

B. When you shift the digits to the right, the left seat of the number is filled by 0

Example:

        document.write (10>>1);        document.write ("<br/>");        document.write ( -10>>1);

Effect:

7.>>>

Unsigned right shift is represented by >>>. It shifts all the numbers in a 32-digit number to the right.

Attention:

A. Unsigned right-shift operation fills all vacancies with 0.

B. For integers, the unsigned right shift is the same as the signed right shift result.

C. For negative numbers, because the left side of 0, resulting in negative numbers after the unsigned right shift, becomes a positive number

For example:

       document.write ( -10>>>1);

Results:

Operation Process:

-10

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0

Unsigned Right Shift -10>>>1

0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1

Results:

2147483643

JavaScript binary Operations

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.