js several special operator slightly Solutions

       Some special applications js operator and tips.

1. contains a specified character: ~

~"str1".indexOf("str2")

Meaning: string str1 being sought

Find the string str2

If not contain str1 str2, the number 0 is returned.

2. Take half: >>

num >> 1

num integer

Example: 50 seeking 1/2

50 >> 1 //25

A little superfluous, not recommended

3. ToSei: |

a | 0

num integer

Example:

1.3 | 0 //1

4. Analyzing Parity: &

num & 1

num integer

Example:

var NUM =. 3 ;
 IF (& NUM. 1 ) { 
  the console.log ( "NUM is an odd number"); // NUM odd 
} the else { 
  the console.log ( "NUM is an even number" ); 
}

The exchange value of two numbers: ^ =

_ Using arrays:

was num1 = 1 num2 = 2 ; 
num1 = [num2, num1 num2 =] [0 ]; 
console.log (num1, num2); // 2 1

_ Use bit exchange operator:

was num1 = 1 num2 = 2 ; 
num1 ^ = num2; 
num2 ^ = num1; 
num1 ^ = num2; 
console.log (num1, num2); // 2 1

n-th power of 6.2: and << **

1 << n-1

Use bit run operators: <<

Example: 2 3 power: 1 << 3 // 8

Use a power running character:

x to the power of y: x ** y

Example: 2 3 power: 2 @ 8 * 3

Method Comparison:

<< ways to support the use of negative ** Requires parentheses otherwise error.
** more intuitive to use, not studied the laws of any number of arbitrary power << method.

eg:

-1 << 3 @ -8 
-2 ** 3 // given 
- (2 ** 3) @ -8 
3 << 2 // 12 Error 
3 ** 2 // 9 proper

and many more

Guess you like

Origin www.cnblogs.com/xy88/p/11421259.html