Exploitation of |, & amp;, ^ ,~, & Lt;, & gt; write highly efficient code

Source: Internet
Author: User

Introduction:

When you read the source code, you will often see some code that is particularly difficult to understand, such as the following.

cancelEvent.setAction(MotionEvent.ACTION_CANCEL |            (motionEvent.getActionIndex()                    << MotionEvent.ACTION_POINTER_INDEX_SHIFT));

order = ((order) >> (INDEX_OFFSET -1) + 1) << INDEX_OFFSET;

Similar to this kind of "tall" code, it can be seen everywhere in the code or source code written by various great gods.

What does this code mean? Why? What are the benefits of such writing? How can I write such code? Let's take a look at the following articles with these questions.

Logical operators:

Logical operators are mainly used to calculate true/false values (boolean), which are usually used in condition judgment statements.

Logical operations are the most commonly used, mainly including &, ||,&, |. I believe everyone is familiar with it, so here is a brief introduction of the difference between & and:

For & |, if the left side is passed, the right side is not judged. And & and | are the equations on both sides that need to be calculated.

(So here is a trick. If you do not need to judge both sides, use & or | and place the computing conditions that require more time on the right, with less time on the left)


Bitwise OPERATOR:

Bitwise operators operate on the binary number (int.

Including: and (&), or (|), non (~), Exclusive or (^), left shift (<), right shift (>>>), positive and negative shift (> ).

And (&): Both sides are 1 (true), and the result is 1 (true). Otherwise, the result is 0 (false)

Example: 1 & 2 = 0001 & 0010 = 0000 = 0

Or (|): One of the two sides is 1 (true), and the result is 1 (true ). Both sides are 0 (false), and the result is 0 (false)

Example: 1 | 3 = 0001 | 0011 = 0011 = 3

Non (~): If the bit is 0 (false), the result is 1 (true). If the bit is 1 (true), the result is 0 (false)

Example :~ 4 = ~ 0100 = 1011 = 11

XOR (^): In the bitwise of the two operands, 0 (false) is the same, and 1 (true) is the same)

Example: 1 ^ 4 = 0001 ^ 0100 = 0101 = 5

Shift left (<): Move the specified number of digits on the right of the operator to the left and add zeros at the low position. Or equal to multiplying (the value on the left) by the power of 2 (the value on the right)

Example: 1 <4 = 0001 move 4 digits to the Left = 10000 = 16 or = 1*2 ^ 4 = 16

Shift right (>>>): Move the number of digits specified on the right of the operator to the right, and add zero at the high position. Or divide the (left value) by the (Right Value) power of 2

Example: 4 >>> 2 = 0100 move 2 to the Right = 0001 = 1 or = 4/2 ^ 2 = 1

Plus or minus right shift (>): Move the number of digits specified on the right of the operator. If it is a positive number, it is set to zero at the high position. If it is a negative number, it is set to 1 at the high position.

Example:

4> 2 = 0100 move to the right 2 digits after the high position 0 = 0001 = 1

-4> 2 =-0100 move to the right 2 digits after the high position fill 1 = 1011 move to the right two digits after the high position fill 1 = 1110 = 14


Use in programsBitwise operation:

First, we need to have a concept. The purpose of binary is to use 1 and 0 as the flag bit. Generally, 1 is regarded as true, and 0 is regarded as false. In this way, multiple flag information can be carried in a variable.

Here is an example. You can see it later.

The requirement is that there are four permission judgments: A, B, C, and D. We need to perform different operations based on different combinations of permissions.

Public static final int PASS_BY_A = 1; // 0001 public static final int PASS_BY_ B = 2; // 0010 public static final int PASS_BY_C = 4; // 0100 public static final int PASS_BY_D = 8; // 1000 public static final int PASS_BY_ABCD = PASS_BY_A | PASS_BY_ B | PASS_BY_C | PASS_BY_D; // 1111/*** @ param args */public static void main (String [] args) {boolean isA = false; boolean isB = false; boolean isC = false; boolean isD = false; int isPass =-1; // obtain the permission and assign values to each permission variable. initParameter () is ignored here; // use the traditional boolean for judgment, whether the four permissions of ABCD are met. if (isA & isB & isC & isD) {}// use binary to determine whether the four permissions of ABCD are met. if (isPass & PASS_BY_ABCD) = 0) {}}
Through comparison, we can see that the use of binary for judgment, variable usage, efficiency, and code simplicity are greatly improved. This is also why binary and bitwise operations are frequently used in various source codes.


Bit operation tips:

Finally, I summarized some tips I found when using binary and bitwise operations.

A | B:Merge 1 in AB.

Example 1: a is 1100 (12), B is 0001 (1), a | B = 1101 (13 ).

Case 2: a = 0001 (1 ). In this case, we want to make a = 1111 (15 ). The simplest way is to find B = 1110 (14 ). A | = B (a = a | B)


A & B:Remove 1 from B from.

Example 1: a is 1010 (10), B is 1000 (8); a & B = 0010 (2 ). If a & B = 0000 (0), it means that the flag spaces of a and B are identical. If a & B! = 0 indicates that AB is different. It is often used for judgment.

Case 2: a = 1111 (15 ). In this case, we want to make a = 1001 (9 ). The simplest way is to find B = 0110 (6 ). A & = B (a = a & B)


A ^ B:Find the different/identical values in AB (1 is different, 0 is the same ).

Example 1: Assume that a is 1100 (12) and B is 0111 (7 ). A ^ B = 1011 (11)


A <B and a> B:Shifts left and right are usually binary values, and change values.

Case 1: a = 0001 (1 ). A <1 = 0010 (2), a <2 = 0100 (4), a <3 = 1000 (8), and a for loop can quickly assign values to binary values.

Example 2: a = 1100 (12), INDEX_OFFSET = 0001 (1 ). A >>>= INDEX_OFFSET = 0110 (6), a >>> = INDEX_OFFSET = 0011 (3 ).

In addition, when we perform multiplication and division by multiples in the program, we 'd better use the left shift and right shift operations, which will increase the efficiency a lot.


Finally, list the Set of 4*4 second-level numbers.

0 = 0000 1 = 0001 2 = 0010 3 = 0011
4 = 0100 5 = 0101 6 = 0110 7 = 0111
8 = 1000 9 = 1001 10 = 1010 11 = 1011
12 = 1100 13 = 1101 14 = 1110 15 = 1111

Two groups of colors are commonly used. If you can, you can back up several groups to avoid current computing, which greatly increases programming efficiency.

Generally, four or four flag bits are enough for the program. 5*5, 6*6, and so on. The more the flag is used, the more obvious the binary advantage is.


Summary & feelings:

I usually do projects and rarely have the opportunity to use algorithms when doing programs. I think binary and bitwise operations can be considered as a simple algorithm, because the purpose of the algorithm is to "decompress" the program, but it will "supercharge" the programmer accordingly ". To avoid Code Monkey or Code Farmer, we should take the initiative to find opportunities to "supercharge" ourselves ". At ordinary times, you have the opportunity to use as many algorithms as possible, learn more about the source code and the programming style of the great gods, improve your skills, and write efficient, elegant, and artistic code!

I believe that after reading this article, you will understand the usage of the flag in the binary program. At least you will not be confused when looking at the source code. In addition, I have only discovered the role of binary in the program as a flag. There may be other applications. If you know this, I hope you can share it with me.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.