In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the methods of using JS bit operators". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what are the methods of using JS bit operators"!
1. Use left shift operator> 0) // 11 console.log(11.71 >> 0) // 11
Note>>> Negative numbers cannot be rounded
6. Use ^to complete the value exchange
Before ES6's deconstructed assignment comes out, it's faster (but it has to be an integer) in this way:
// --- before --- let temp = a; a = b; b = temp; //Traditional, but with the help of temporary variables b = [a, a = b][0] //by array //--- after ---can only be used for integers let a = 7 let b = 1 a ^= b b ^= a a ^= b console.log(a) // 1 console.log(b) // 7 [a, b] = [b, a]; // ES6, deconstructing assignment 7. Use ^to determine if the symbols are the same
Only integer and can be applied
(a ^ b) >= 0; // true same; false different 8. Use ^to check if numbers are unequal//--- before --- if (a !== 1171) {...}; // --- after --- if (a ^ 1171) {...}; 9. n & (n - 1), if 0, n is an integer power of 2
10. Use A + 0.5| 0 instead of Math.round()
String1. Use toString(16) to get a random string Math.random().toString(16).substring(2, 15);
The second parameter of.substring() controls how many bits it takes (up to 13 bits)
2. Use split(0)
Using a number as a separator for a split saves 2 bytes (not practical)
// --- before --- "alpha,bravo,charlie".split(","); // --- after --- "alpha0bravo0charlie".split(0);3. Create a link using.link()
A little-known way to quickly create a tag
// --- before --- let b = `google`; // --- after --- let b = google .link( www.google.com );4. Use Array to repeat characters // --- before --- for (let a = "", i = 7; i--;) a+= 0; // --- after --- let b = Array(7).join(0); // "0000000"let c = Array(7).join( La ) // "LaLaLaLaLaLa" // ES6 let d = "0".repeat(7); // "000000" Other fancy operations 1. Create a random number using the current time // --- before --- let b = 0 | Math.random() * 100 // --- after --- let a; a = new Date % 100; //two random numbers a = new Date % 1000; //3-digit random number a = new Date % 10000; //4-digit random number... so on //Do not use it in fast loops, as milliseconds may not change;2. Some actions that can replace undefined
3. Empty array with Array.length = 0 4. Use Array.slice(0) to implement shallow copy of array 5. Use!+ "1" Quick judgment of browsers below IE8
6. for Short for loop condition // --- before --- for(let i = 0; i < arr.length; i++) {...} // --- after --- for(let i = arr.length; i--;) {...} //Note that the semicolon behind i--does not miss this. I believe that everyone has a deeper understanding of "what are the methods of using JS bit operators". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.