In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how js bit operation is used in practice, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use js bit operation in practice. Let's take a look.
What is a bit operation?
All data from modern computers is stored in binary form in the device. That is, in the two states of 0 and 1, the operation of the computer on the binary data (+, -, *, /) is called bit operation, that is, symbol bits participate in the operation together.
There are 6 bitwise operators
&: bitwise and\
|: bitwise or\ |
^: bitwise XOR\
~: bitwise reverse\
> >: move to the right\
3.base image2 build complete-> 4. Base image3 build complete-> 5. Complete / fail
When we check the status: it is possible that this situation occurs:
The task is completed by base image2 build, but it should also be queried when we query base image1 build completion.
The task is completed, and it should also be queried when we query the base image3 build to complete it.
Is it troublesome for us to store state enumeration values in the database?
Solution one:
Then the bit operation is more appropriate, as defined by the following enumeration:
{waiting: 0b000001, image1Finish: 0b000010, image2Finish 0b000100, image3Finish 0b001000, finish: 0b010000, fail: 0b100000,}
Then the above [type 1] situation can be expressed as: 0b000001 | 0b000010 | 0b000100 (waiting | image1Finish | image2Finish) bitwise or
When we decide whether we are in image2Finish or not, we can status & image2Finish = image2Finish.
Status = 0b000001 | 0b000010 | 0b000100 = 0b000111status & image2Finish = 0b000111 & 0b000100 = 0b000100 (image2Finish)
Isn't that easy to judge? At the same time to improve their own code pressure grid? Reduce a lot of if else writing
Method 2:
In fact, there is another way is the prime number representation:
{waiting: 3, image1Finish: 5, image2Finish 7, image3Finish 11, finish: 13, fail: 17,}
It is also the first case above:
Status = 3 * 5 * 7 beat / judge state: modular operation isImage2Finish = status% 7 (image2Finish) = 0: comprehensive application of bit operation
Here is an example-- do not use addition, subtraction, multiplication and division to do addition, often used to examine the mastery of counterpoint operations. Readers can first try to analyze and implement it.
You can't use addition, subtraction, multiplication and division, which means you have to calculate it with a bit operation. A practical example is given, such as a = 81 = 0b1010001, b = 53 = 0b0110101. Through the XOR operation, we find that XOR adds two numbers but cannot carry, and through the and operation, we can know which bits need to carry, as follows:
1010001 ^ 0110101-1100100 1010001 & 0110101-0010001
It should not be difficult to understand that moving the value obtained through the and operation one bit to the left and adding it to the value obtained through XOR is equivalent to realizing carry. In order to achieve the addition of these two numbers, this process can be repeated: XOR, and, and then carry, until no more carry is needed. So it's not difficult to write the following code:
Function addByBit (a, b) {if (b = = 0) {return a;} / non-carry additive let c = a ^ b; / / record the let d = a & b; d = d 6); return running_hash;}
Constantly accumulate the ASCII value of the current string, which uses XOR, move left and move right.
This is the end of the article on "how js bit operations are used in practice". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use js bit operations in practice". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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: 237
*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.