Exploitation of |, &, ^ ,~, <, >>> Write highly efficient code

Source: Internet
Author: User
Tags bitwise operators

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.


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.