JS bitwise operator

Source: Internet
Author: User
Tags bitwise operators

Bit arithmetic not
The bitwise operation not is represented by a negative number (~), which is one of the few operators related to binary arithmetic in ECMAScript.

Bitwise arithmetic NOT is a three-step process:

1. Convert the operand to a 32-digit number
2. Convert binary number to its binary counter code
3. Convert binary numbers to floating point numbers
For example:

var iNum1 = 25; 25 equals 00000000000000000000000000011001.
var iNum2 = ~inum1; Convert to 11111111111111111111111111100110
alert (INUM2); The output "26" bit operation is not essentially negative for the number, then minus 1, so 25 becomes 26. The same approach can be obtained using the following method:

var iNum1 = 25;
var iNum2 =-inum1-1;
alert (INUM2); Output-26-bit operation

Bitwise arithmetic AND
Bitwise operation and by ampersand (&), directly to the binary form of the number of operations. It aligns the digits in each number and then uses the following rule to perform an and operation on the two digits in the same position:

The digit result in the second digit of a digit in the first digit
1 1 1
1 0 0
0 1 0
0 0 0

For example, to perform an and operation on numbers 25 and 3, the code looks like this:

var iresult = & 3;
alert (Iresult); Output "1" 25 and 3 for the and operation result 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) is stored in 1, so the other digits generate 0, so the result is 1.

Bitwise Operation OR
The bitwise operation OR is represented by the symbol (|), and is also the direct operation of the binary form of the number. When each bit is computed, the OR operator takes the following rules:

The digit result in the second digit of a digit in the first digit
1 1 1
1 0 1
0 1 1
0 0 0

Still use the AND operator for the example, the OR operation on 25 and 3, the code is as follows:

var Iresult = 25 | 3;
alert (Iresult); Output "27" 25 and 3 for the OR operation result 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

As you can see, in two numbers, a total of 4 digits are stored in 1, and these digits are passed to the result. Binary code 11011 equals 27.

Bitwise Operation XOR
The bitwise operation XOR is represented by the symbol (^), and of course, it is also directly in binary form. XOR differs from OR, and it returns 1 when only one digit is stored at 1 o'clock. The truth table is as follows:

The digit result in the second digit of a digit in the first digit
1 1 0
1 0 1
0 1 1
0 0 0

For XOR operations of 25 and 3, the code is as follows:

var Iresult = 25 ^ 3;
alert (Iresult); The result of the XOR operation for output "26" 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

As you can see, in two numbers, a total of 4 digits are stored in 1, and these digits are passed to the result. Binary code 11010 equals 26.

Left shift operation
The left shift operation is represented by a two less than sign (<<). It moves all digits in the number to the left by the specified number. For example, shift the number 2 (equal to 10 in binary) 5 bits to the left and the result to 64 (equal to 1000000 in binary):

var iOld = 2; equals binary 10
var iNew = iOld << 5; equals binary 10 million binary 64 note: When you move the digits to the left, there are 5 extra slots to the right of the number. The left-shift operation fills these slots with 0, making the result a complete 32-digit number.

Note: The left shift operation retains the sign bit of the number. For example, if you move 2 to the left 5 bits, you get 64 instead of 64. "is the symbol still stored in 32nd place?" "Yes, but this is done in the background of the ECMAScript, and developers cannot access the 32nd digit directly. Even if you output a negative number in the form of a binary string, the negative sign is displayed (for example, 2 will display-10. )

Signed right shift operation
The signed right-shift operator is represented by a two greater-than sign (>>;). It shifts all the digits in a 32-digit number to the right, while preserving the number's sign (plus or minus). The signed right-shift operator is exactly the opposite of the left-shift operation. For example, moving 64 to the right 5 bits will change to 2:

var iOld = 64; equals binary 1000000
var iNew = iOld >> 5; equals binary 100 binary 2 Similarly, moving the digits will result in empty space. This time, the vacancy is on the left side of the number, but after the sign bit. ECMAScript fill these slots with the value of the sign bit, creating the complete number as shown in:

Unsigned Right shift operation
The unsigned right-shift operator is represented by three greater-than sign (>>>), which shifts all digits of the unsigned 32-digit number to the right. For positive numbers, the result of an unsigned right-shift operation is the same as a signed right-shift operation.

Using the example in the signed right shift operation, move 64 to the right by 5 bits, which will change to 2:

var iOld =;  // equals binary 1000000 var // is equal to binary 100 binary 2 for negative numbers, the situation is different. 

Unsigned right-shift operation fills all vacancies with 0. For positive numbers, this is the same as a signed right-shift operation, and a negative number is treated 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 the negative numbers is always a very large one. For example, if you move 64 to the right 5 bits, you will get 134217726. What if we get this result?

To do this, you need to convert this number to an unsigned equivalent (although the number itself is still signed), you can obtain this form by using the following code:

var iUnsigned64 = -64 >>> 0;

Then, the number type toString () gets its true bit representation, using a base of 2:

Alert (iunsigned64.tostring (2));

This generates a binary complement representation of 11111111111111111111111111000000, the signed integer-64, but it equals the unsigned integer 4294967232.

For this reason, use the unsigned right-shift operator with caution.


Tips for the following common JavaScript bit operators

JS bit arithmetic to judge odd even

if (n&1===0) {// even } Else {// Odd }

JS bitwise operator to replace Math.floor

(2.9|0) ===2(~~2.9) ===2(2.9>>>0) ===2(2.9>>0) ===2(2.9<< 0) ===2// Note ~ ~ (-2.999); // =-2 Math.floor (-2.999); // = -3rgb2hex function Rgb2hex (a,b,c) {return"#" + ((256+a<<8|b) <<8|c). ToString (+). Slice (1)}

Or

functiontohexstring (r,g,b) {return("00000" + (R << | g << 8 | b). toString (+)). Slice (-6);}varHex =tohexstring (red, green, blue); detecting equality relationshipsif(a!=123)if(a^123)//Note:false^ 1//1true^ 1//02 ^ 1//3{} ^ 1//1 This is very useful, such as the following codevarIsReady = 0; //somewhere elseif(IsReady) {}//somewhere else, set IsReady state to 1IsReady ^= 1; //somewhere else, set IsReady state to 0IsReady ^= 1; default Valueif(!n) n =Defaultvalue;n|| (n=defaultvalue); IndexOf and~

You often use String.IndexOf in your code, for example:

' abc '. INDEXOF (' d ') ===-1; if (' abc '. INDEXOF (str)!==-1) {// in String }

We can write it like this.

~ ' abc '. INDEXOF (' d ') ===0; if (~ ' abc '. INDEXOF (' d ')) {// in string } if (!~ ' abc '. INDEXOF (' d ')) {// not in string }

Of course, JavaScript's bitwise operators can cause code to be less readable, so weigh in when you use them.

JS bitwise 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.