In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Official account of Wechat
Bit operation is the basis of the programming language, you will see a lot of bit operation code when looking at the source code, but rarely see the operation in place in the project code. Because in the application code, there are a lot of judgments and calculations can be directly completed with numerical judgment and calculation, there is no need to use bit operation, so that these basic things are slowly used less and less, and gradually forgotten. One result is that it is very difficult to read the source code, because a large number of bit operation logic, do not understand.
As a programmer, I feel that data bit operation is very necessary, a bit as follows:
When looking at the source code, we can better understand the habit of bit operation closer to the computer, and the execution efficiency will be more efficient. Bit operation is used in the project, which reflects the basic bit operation of forced lattice N-and operator (&).
Operation rules:
Operand 10011 Operand 20101 and operation 0001
Rule summary: the result is 1 only when both operands are 1, and the rest is 0. In other words, as long as it contains 0, it is 0.
The operation is as follows:
Bit operation-- or operator (|)
Operation rules:
Operand 10011 Operand 20101 or operation 0111
Rule summary: the result is 0 only when both operands are 0, and the rest is 1. In other words, as long as it contains 1, it is 1.
The operation is as follows:
Bit operation-- non-operator (~)
Operation rules:
Operand 10 or operation 01
Rule summary: reverse operation, 1 to 0, 0 to 1.
Bit operation-XOR operator (^)
Operation rules:
Operand 10011 Operand 20101 or operation 0110
Rule summary: if the Operand is the same, the result is 0, otherwise the Operand is 1.
Bit operation-signed displacement operator (> >, > 2x2. This is the same result as the signed displacement.
The negative number moves to the right (>)
Take-10 > > 2 as an example.
-10purl 11111111 11111111 11111111 11110110
After moving to the right, the space on the left is filled with 0, note that it is not filled with 1, and the reason is explained later.
00111111 11111111 11111111 11111101
This result is very big, the result is 1073741821, a negative number has become such a big negative number, do not doubt your eyes, this result is correct.
Summary
The so-called unsigned right shift is to directly move the original binary value to the right to get the result, whether it is a negative or positive number, there is no complement operation, the complement is unified to use 0, rather than the corresponding symbol bit 1 or 0.
Now that the N basic bit operations are over, let's take a look at a few examples and some techniques commonly used in the code.
Commonly used bit operations are used to determine whether a number is odd or even. / / true is represented as odd, false is expressed as even public boolean checkNum (int num) {return (num&1) = = 1;}
The binary of 1 is 00000000 00000000 00000000 00000001 & the rule of the operation is that the result is only 1. Obviously, no matter what number it is, the first 31 bits are 0, and only the last bit may get a different result. The last digit of the even number must be 0 and the odd number must be 1, so the result is obvious.
Do not use intermediate variables, exchange two values (common in interviews) / / the incoming axiom 3direction benda5public void swap (int Aminint b) {a ^ = b / b = b ^ b; a ^ = b / b = b ^ a; a ^ = b / b / a = a ^ b; System.out.println ("a =" + a); System.out.println ("b =" + b);} / * output result:
This is flexibly implemented using XOR. Let's make a simple explanation.
A = 3PUR 00000011
B = 5PUR 00000101
The result of a XOR b is: 00000110, which is assigned to a
The result of b XOR an is: 00000011, which is assigned to b
The result of a XOR b is: 00000101, which is assigned to a
So the final result is:
A = 5PUR 00000101
B = 3PUR 00000011
Take the absolute value public int getAbs (int n) {return (n ^ (n > 31))-(n > > 31);}
Not to be specific, you can try to write it yourself. (because it involves examples of positive and negative numbers, and then negative numbers have complement operation, so there is too much to write, so this blogger will not write it!)
In fact, there are many examples here, not all of them. If you are interested, you can take a look at the related blogs. There are many articles.
Practical project experience operation
Related to the company's things, here will not be very specific, give ideas.
Now there is an order, the order can carry out many operations, such as: create, modify, return the order, accept the order, terminate, dispatch, submit completion and so on. At the same time, the order also has a lot of status, such as: pending order, in progress, terminated, completed and so on. The current scenario is that different states correspond to different operations, and the front end needs to display different operation controls according to different states.
Idea 1:
The simplest and most troublesome is to use N flag fields to represent the operations that can be performed, and when returning data, assign values true and false to each flag field, indicating whether this operation can be done. However, there is a huge problem. When an operation is removed or several new operations are added in the later stage, it involves the deletion and addition of fields, with large changes, poor maintainability and inflexibility.
Idea 2: (recommended)
Expressed in this binary way. The implementation is as follows:
The first place: 0Phone1 indicates whether it can be modified
The second place: 0prime1 indicates whether you can take the order.
The third place: 0Pol 1 indicates whether it can be terminated.
Only one field tags is needed to return it to the front end. If the corresponding value is 010, it means that it cannot be modified, accepted or terminated.
If you don't want to terminate the operation now, then the binary bit is always 0. If a new commit operation is added, let the fourth digit indicate whether it can be submitted. There is no need to add a field, just add a bit to the original field and OK it.
There are many other application scenarios.
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.