text

js The most notorious Bug namely   0.1 + 0.2  != 0.3 , With java equally , Because of the accuracy , This makes all floating point operations unsafe !

All the people suggested not to be in the js Using bit operations in ,

If js It's better to use it , One bit operation can be used to improve the performance , okay , Not much , Go straight to the subject :

1. Bit operation

use   <<   operator , You can quickly calculate one 2 The power of the integer number of , If the decimal is written, the decimal will be ignored automatically
1 << 2; //4 2 Of 2 Power 1 << 10 ;//1024 2 The tenth power of 1 << 2.5 ;//4 still 2 Of 2 Power , If there are decimals after it , The decimal will be removed automatically
2. use & To judge odd , even numbers

Odd number & 1 = 1

even numbers & 1 = 0
// Odd number console.log(7 & 1); //1 // even numbers console.log(10 & 1); // decimal
Or automatically ignore decimals , If it is before the decimal point, it will be returned 1, If it's even, return it 0 console.log(6.5 & 1);//0
3. use !! Converts a number to a Boolean value

!! wrong 0 That's the value of true, If it is 0 Just false
console.log(!!6);//true console.log(!!0);//false

4. use ~~ ,>> ,<<  ,>>> ,| Fast rounding

Remove the number immediately after the decimal point . >>>  Negative numbers cannot be rounded
// Positive number console.log(~~12.7); //12 console.log(12.7>>0);//12
console.log(12.7<<0);//12 console.log(12.7>>>0);//12 console.log(12.7|0);//12
// negative console.log(~~-12.7|0);//-12

5. use ^ Fast exchange value

Quickly exchange the values of two variables
// The traditional way This is done with a third temporary variable let a= 1; let b=8; let c=1; c = b ;//c=8 b = a ;//b=1 a
= c ;//a=8 console.log(a)//8 console.log(b)//1 // use ^ , You don't need a third temporary variable to store the value let a =
1; let b = 8; a ^= b; b ^= a; a ^= b; console.log(a);//8 console.log(b);//1
6. use ^ To determine whether the numbers are equal
let a = 1025; //c Conventional judgment if(a != 1024){ console.log(" Unequal ") } // use ^ judge if(a ^
1024){ console.log(" Unequal ") }

7.n & (n - 1), If 0, explain n yes 2 Integer power of
let h = 96; h & (h-1) 64 let h = 56; h & (h-1) 48 let h = 48; h & (h-1) 32 let
h = 32; h & (h-1) 0

Colored eggs

Enter the following code on the console
(!(~+[])+{})[--[~+""][+[]]*[~+[]]+~~!+[]]+({}+[])[[~!+[]]*~+[]]

Technology
Daily Recommendation