Javascript binary computation tips _ javascript skills

Source: Internet
Author: User
Some tips on binary operations in javascript are provided to share with you, hoping to help you 1. Original code, reverse code, and complement code, positive and negative subtraction to complement code addition
Javascript uses a 32-bit binary integer for binary operations. Because JavaScript integers are all signed numbers, the highest digit 0 indicates a positive number, and 1 indicates a negative number, the integer expression range used in js binary operations is

The Code is as follows:


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


Original code: the highest bit 0 indicates positive, 1 indicates negative, and the other 31 digits are the binary form of the absolute value of the number (the absolute value of the true value ).
Anticode: the positive number anticode is the same as the original code. The negative number anticode is the same as the original code symbol bit, and the other 31 digits are reversed (0 changes to and 0)
Positive complement: the positive complement code is the same as the original code, and the negative complement code is the inverse code plus 1 (the symbol bit is involved in the computation. In fact, only the-0 complement Code involves the highest bit carry, therefore, you do not need to worry about making-change + when the symbol bit is involved in the carry operation when the anti-code is added with 1 ).
+ 0 backend code: 32 backend codes are processed by positive numbers. The original codes, backend codes, and supplementary codes are all 0.
-0 reverse code: the highest bit is 1, and the remaining bit is reversed by the + 0 original code. 32 ones are obtained.
-0 complement: the anti-code is 32, 1 plus 1, and the maximum overflow is discarded, resulting in 32 zeros.
Therefore, the complement codes of positive and negative 0 are all 0.
Calculate the absolute value of a negative number by the complement code: the absolute value of a negative binary number, as long as you (including the symbol bit) Take the inverse and Add 1, the absolute value is obtained.
When processing addition and subtraction operations, the computer uses the complement code for calculation. Subtraction is regarded as adding a negative number. when processing a negative number, the computer can use the complement code of a negative number for addition to obtain the correct calculation result, the complement code is generated to unify addition and subtraction operations.
The principle of positive subtraction to complement addition is 32-digit overflow:
For a 32-bit binary positive integer, the modulo is

The Code is as follows:


Math. pow (2, 32) = 4294967296


The maximum expression range of a 32-bit positive integer is 4294967296-1. When the value reaches 4294967296, it must be carried to 33 bits. The 33-bit overflow is discarded, only 32 zeros are obtained (this principle is the same as that of the 0 and 12 o'clock hands on the dial, and the dial is 12 as the model). Therefore, A number increases gradually. Once the number M exceeds 4294967296-1, it can be expressed as M % 4294967296.
Negative-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 negative complement code. The negative complement Code is based on the 32-bit binary number, the sum of the original code is just equal to the model). The principle is the same as that of the dial. The 11 points and the negative 1 point are in the same position.
Take-3 as an example:

The Code is as follows:


(Array (32 ). join ("0") + (3 ). toString (2 )). slice (-32); // |-3 | binary number, that is, the original code
Source code = 00000000000000000000000000000011;
Anti-code = 11111111111111111111111111111100; // the original code symbol bit is 1, and the rest are reversed.
Complement = 11111111111111111111111111111101; // The Anti-code is added to 1 because the anti-code is obtained from the low 31 bits of the original code in positive number form. Therefore, the 31 bits of the two numbers are all 1, and the anti-code symbol is 1, get 32 1
So, there are
Complement + original code = (reverse code + 1) + original code
= (Reverse code + original code) + 1
= 1 + (32 bits are all the binary numbers of 1) // because the anticode is obtained from the low 31 bits of the original code in the positive number form by adding the symbol 1, therefore, the low 31 bits of the sum of the two numbers are all 1, and the bitwise Sign 1 is added to get 32 1
= Math. pow (2, 32)
= 4294967296 // This is the 32-bit binary number modulo, same as |-1 | + 11 = 12


This is the process of positive subtraction> negative addition> complement addition.
2. bitwise operations
Because JavaScript integers are positive integers by default, only 31 bits can be used in operations. Developers cannot access the highest bits.
Bitwise operations only occur on integers. Therefore, a non-floating point will be rounded down before being involved in bitwise operations.
To avoid access to the symbol bit, javascript converts it to the binary value of the symbol and its absolute value in the binary value of the actual negative number, for example:

The Code is as follows:


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


Bitwise inversion (~) : Unary operation, 1 to 0 to 1, such
~ 123; //-124
You can verify this process: the positive number is reversed, and the symbol bit is negative. Therefore, the result is a negative number, which can be expressed as-M according to Math. pow ()-M. It can be calculated as follows:

The Code is as follows:


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


It should be noted that the javascript bit operations are signed, so 32 bits are reached, and the highest bits will be used as the symbol bit. A positive number should be obtained when the inverse operation is performed (Modulo Math. fill Number of pow () -- sum of two numbers to obtain a modulo, that is, the two numbers complement each other ).
Bitwise inversion of integer M can be calculated as follows:

The Code is as follows:


(-1 * M-1) + Math. pow () % Math. pow)


Bitwise AND (&): returns 1 if the two numbers have the same bitwise. Otherwise, 0 is returned.

The Code is as follows:


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


The Code is as follows:


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


Bitwise OR (^): The same bit of two numbers. If one is 1 and the other is 0, 1 is returned. Otherwise, 0 is returned.

The Code is as follows:


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


Some features of the exclusive or operation:

The Code is as follows:


A ^ a = 0
A ^ B = B ^
A ^ B ^ c = a ^ (B ^ c) = (a ^ B) ^ c
A ^ B ^ a = B
D = a ^ B ^ c can introduce a = d ^ B ^ c // This feature is used in some encryption algorithms
// If the number of operations is in the appropriate range (No overflow), for example, 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


The bitwise exclusive or operation records multiple information using one digit:
There are several status values: 1, 2, 8, 16 .....
The rule of these values is that only one of their binary values is 1, and the other values are 0. Therefore, the bitwise exclusive or calculation results of any of them do not show that one of the two numbers is 1, and the values of the calculation are unique, that is, knowing the result of the Operation knows the combination of the numbers, so that multiple pieces of information can be recorded with a single number.

The 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 that contains several status values, we can use bitwise OR,
For this example, you can refer to several constants about the image type in PHP and several constants about the PHP error level definition.
This example may be described in decimal number. For example, a single digit represents the state of an attribute, and a ten digit represents the state of another attribute, each status can have 10 values. There are many combinations that can be described with only one number.
Left shift (<): all bits of a binary number move to the left, the sign bit does not move, the high overflow is discarded, and the low position is supplemented by 0.
If no overflow occurs, the Left shift is multiplied by 2.
Right Shift (>): all bits of a binary number move to the right. The sign position does not move. The value is 0 for the high position and discarded for the low position.
The right shift operation is divided by 2 and rounded down.
Shift to the right (>>>): When the shift is performed, the symbol bit follows the movement, and the symbol bit is also treated as a numerical value. Therefore, the result of this operation is a 32-bit unsigned integer, therefore, a positive integer is generated when the positive number is shifted to the right. The positive number shifted to the right is the same as the unsigned one. This is the only operation that can operate on the symbol bit.
-123> 1; // 2147483586
Notes:
The bitwise operation must be an integer. If the bitwise operation element is not an available integer, 0 is used as the bitwise operation element.

The Code is as follows:


~ NaN; // execute ~ 0. The result is-1.
~ 'X'; //-1
'Hello' | 0; // 0
({}) | 0; // 0


The displacement operation cannot move more than 31 digits. If you try to move more than 31 digits, perform the modulo operation on 32 digits before moving.
123> 32 // actually 123> 0 (32% 32 = 0)
123> 33 // actually 123> 1
The 32-bit signed integer is expressed in the range of-Math. pow )~ Math. pow (2147483648)-1 ~ 2147483647, while the js number precision is double precision, 64-bit. If an integer over 2147483647 is involved in bitwise calculation, You need to note that its binary overflows. After intercepting 32-bit, if the first digit is 1, it is interpreted as a negative number (complement ).

The Code is as follows:


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

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.