Ecmascript bitwise Operator

Source: Internet
Author: User
Tags bitwise operators

Bitwise operators operate at the bottom of a number (that is, 32 digits of a number.

Repeat Integers

Ecmascript integers are of two types: signed integers (positive and negative values are allowed) and unsigned integers (only positive numbers are allowed ). In ecmascript, All integers are signed integers by default. What does this mean?

A signed integer uses 31 bits to represent the value of an integer, 32nd bits to represent an integer, 0 represents a positive number, and 1 represents a negative number. The value range is-2147483648 to 2147483647.

You can store signed integers in binary format in two different ways. One is used to store positive numbers and the other is used to store negative numbers. Positive numbers are stored in real binary format. Each bit in the first 31 represents the power of 2, starting from 1st bits (bit 0), indicating 20, 2nd bits (bit 1) indicates 21. The useless bits are filled with 0, which is ignored. For example, the expression of number 18 is displayed.

The binary version of 18 only uses the first five digits, which are the valid digits of this number. Convert the number into a binary string to see the valid bit:

VaR inum = 18; alert (inum. tostring (2); // output "10010"

This Code only outputs "10010", not the 32-bit expression of 18. Other digits are not important, because only the first five digits can be used to determine the decimal value. As shown in:

Negative numbers are also stored as binary code, but the format is binary complement. You can perform the following three steps to calculate the binary complement:

  1. Determine the binary representation of the non-negative version of the number (for example, to calculate the binary complement of-18, first determine the binary representation of 18)
  2. Returns the binary anticode, that is, to replace 0 with 1 and 1 with 0.
  3. Add 1 to the binary anti-code

To determine the binary representation of-18, you must first obtain the binary representation of 18, as shown below:

0000 0000 0000 0000 0000 0000 0001 0010

Next, calculate the binary anticode as follows:

1111 1111 1111 1111 1111 1111 1110 1101

Finally, add 1 to the binary anti-code, as shown below:

1111 1111 1111 1111 1111 1111 1110 1101                                      1---------------------------------------1111 1111 1111 1111 1111 1111 1110 1110

Therefore, the binary representation of-18 is 1111 1111 1111 1111 1111 1111 1110 1110. Remember, when processing signed integers, developers cannot access 31 characters.

Interestingly, after converting a negative integer into a binary string, ecmascript is not displayed in the form of a binary complement, but is output in the form of a negative sign before the standard binary code of the absolute value of the number. For example:

VaR inum =-18; alert (inum. tostring (2); // output "-10010"

This code outputs "-10010" instead of a binary complement code, which is used to avoid access to bit 31. For ease, ecmascript processes integers in a simple way, so that developers do not have to care about their usage.

On the other hand, the unsigned integer processes the last digit as another digit. In this mode, the 32nd BITs do not represent the number, but the value is 231. Because of this extra bit, the value range of the unsigned integer is 0 to 4294967295. For integers smaller than 2147483647, the unsigned integer seems to be the same as the signed integer, while an integer greater than 2147483647 uses 31 (in a signed integer, this is always 0 ).

After an unsigned integer is converted to a string, only the valid bits are returned.

Note: All integers are stored as signed integers by default. Only the bitwise operators of ecmascript can create unsigned integers.

Bit operation not

Bit operations not by a negative number (~) It is one of the few operators related to binary arithmetic in ecmascript.

Bit operations are not three steps:

  1. Converts an operation number to a 32-bit number.
  2. Converts a binary number to its binary anticode.
  3. Converts a binary number to a floating point number.

For example:

VaR inum1 = 25; // 25 is equal to 255.255.255.255.255.00011001var inum2 = ~ Inum1; // convert it to 11111111111111111111111111100110 alert (inum2); // output "-26"

The bitwise operation not is to calculate the negative value for the number and then subtract 1, so 25 is changed to 26. The following method can also be used to obtain the same method:

VaR inum1 = 25; var inum2 =-inum1-1; alert (inum2); // output-26
Bitwise operation and

Bitwise operations and are expressed by ampersand (&), which directly performs operations on the binary form of numbers. It alignment the digits in each number and then uses the following rules to perform and operations on the two digits in the same position:

Number in the first digit
Number in the second digit
Result

For example, to perform and operations on Numbers 25 and 3, the Code is as follows:

VaR iresult = 25 & 3; alert (iresult); // output "1"

The result of the and operation on 25 and 3 is 1. Why? The analysis is as follows:

25 = 0000 0000 0000 0000 0000 0000 0001 1001  3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------AND = 0000 0000 0000 0000 0000 0000 0000 0001

It can be seen that in 25 and 3, only one digit (bit 0) Stores 1, so other digits generate 0, so the result is 1.

Bitwise operation or

Bitwise OR is expressed by a symbol (|). It is also a binary operation of a number. When calculating each user, the OR operator follows the following rules:

Number in the first digit
Number in the second digit
Result

Use the and operator as an example to perform or operations on 25 and 3. The Code is as follows:

VaR iresult = 25 | 3; alert (iresult); // output "27"

The result of performing the or operation on 25 and 3 is 27:

25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011--------------------------------------------OR = 0000 0000 0000 0000 0000 0000 0001 1011

It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11011 equals 27.

Bitwise operation XOR

The bit operation XOR is expressed by the symbol (^). Of course, it is also a direct binary operation. XOR is different from or. When only one digit stores 1, it returns 1. The truth table is as follows:

Number in the first digit
Number in the second digit
Result

Perform XOR operations on 25 and 3. The Code is as follows:

VaR iresult = 25 ^ 3; alert (iresult); // output "26"

The result of performing XOR operations on 25 and 3 is 26:

25 = 0000 0000 0000 0000 0000 0000 0001 1001  3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------XOR = 0000 0000 0000 0000 0000 0000 0001 1010

It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11010 equals 26.

Left shift operation

The Left shift operation is represented by two smaller signs (<). It moves all digits in a number to the left. For example, if the number 2 (equal to 10 in binary) is shifted to 5 digits, the result is 64 (equal to 1000000 in binary ):

VaR iold = 2; // equals to binary 10var inew = iold <5; // equals to binary 1000000 decimal 64

Note: five more spaces are available on the right of the number when the number is left shifted. The Left shift operation fills these spaces with 0, making the result a complete 32-bit number.

Note: The left shift operation retains the symbol bit of the number. For example, if you move-2 to the left to 5 digits, the result is-64 instead of 64. "Is the symbol still stored in the 32nd-bit format ?" Yes, but this is done in the ecmascript background. Developers cannot directly access 32nd digits. Even if a negative value in the form of a binary string is output, it is displayed as a negative number (for example,-2 will display-10 .)

Bitwise operation

The shifted right operator is represented by two greater signs (> ). It shifts all the digits in the 32-bit number to the right and retains the symbol (positive or negative) of the number ). The bitwise shift operator is the opposite of the bitwise shift operator. For example, to shift 64 to the right, change to 2:

VaR iold = 64; // equals to binary bytes 00var inew = iold> 5; // equals to binary 10 decimal 2

Similarly, after the number is moved, the space is vacant. This time, the blank space is located on the left side of the number, but behind the symbol bit. Ecmascript fills these spaces with the symbol bit values and creates a complete number, as shown in:

Unsigned right shift operation

The unsigned right shift operator is represented by three greater than signs (>>>). It shifts all digits of the unsigned 32-digit number to the right. For positive numbers, the result of the unsigned right shift operation is the same as that of the signed right shift operation.

In the example of a signed right shift operation, change 64 to 5 bits to 2:

VaR iold = 64; // equals to binary bytes 00var inew = iold >>> 5; // equals to binary 10 decimal 2

For negative numbers, the situation is different.

The unsigned right shift operation fills all spaces with 0. For positive numbers, this is the same as the operation for the signed right shift operation, while the negative number is processed 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 negative numbers always produces a very large number. For example, if you shift-64 to the right by 5 digits, 134217726 is returned. How can we get this result?

To achieve this, you need to convert the number into an equivalent form without a symbol (although the number itself is still signed). You can obtain this form using the following code:

var iUnsigned64 = -64 &gt;&gt;&gt; 0;

Then, use the tostring () of the number type to obtain its true bit representation. The base is 2:

alert(iUnsigned64.toString(2));
This will generate 11111111111111111111111111000000, that is, the binary complement representation of the signed integer-64, but it is equal to the unsigned integer 4294967232.

For this reason, be careful when using the unsigned right shift operator.

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.