Java one thousand Q: Java-bit arithmetic classic application (a)

Many people believe that bit computing inoffensive in the actual development process, learning bit operation just to cope with the interview. This idea is wrong, then we will pass several series of articles to introduce several classic examples of application-bit computing in the actual development process. If you master the rules of operation of the bit is not very skilled, you can read " the Java one thousand Q: Java language bitwise explain ." This article explains in detail not only the basic rules of the Java-bit arithmetic operations and some common law while still article mentions some common bit computing practical applications, such as ways to use bit arithmetic operations quickly to a variable where the memory clearing unit, or bit operation manner like a variable fast multiplication. But these practical applications mentioned in this article are relatively simple, this series of articles will tell you that the more complex and practical application classic case. In addition, for better reading results, please read this article first before the reader master the "complement" of the calculation rules.

First, the parity judgment integer

According to the conventional thinking, a parity is determined by using the integer number of 2 modulo calculation to see whether the result is zero. Bitwise operators learned later, we can change the thinking to consider the issue. We know: Java language, all numbers stored in memory, it has to be converted into the form of complement. After any even number complement represented by its last bit is 0, and the last bit is a complement odd. So, we can judge by the last digit binary number complement of the integer is 0 or 1, to determine this number is even or odd. The method of determination is carried out with the number 1 and bitwise operations, if the result is 0, then this number is even, otherwise, it is odd. If we do not understand the principles of this algorithm, see below:

Java one thousand Q: Java-bit arithmetic classic application (a)
To facilitate the presentation, we want to determine the parity of numbers called a. Figure, the horizontal line as the boundary, respectively, show a case where an even and odd, the result of bitwise AND operation with the number 1. Wherein the binary string is above a complement, the following is a binary string of 1's complement numbers. It can be seen that, after being converted into a digital 1's complement, a total of 32, which are 0, the final value of a first 31 bit is 1. This causes the front 31 either 0 or 1 a complement, carried out in the previous 31-bit operation, the operation result can only be 0, to determine the final result of the operation is only a complement to the digital 1's complement the rightmost bit. If the rightmost one who is 0, then a is an even number, and a 1 bit and operation of the end result is the number 0. Conversely, if the rightmost one who is 1, then a is an odd number, and a 1 bit and operation of the final result is the number 1. So long as we look at the figures 1 and a bitwise AND operation to know a parity of. To achieve the following specific procedures:

Java one thousand Q: Java-bit arithmetic classic application (a)

Second, the absolute value

Conventional thinking is the absolute value algorithm: a first determining whether a number> = 0, if a> = 0, then a return to itself, a number of otherwise contrary. This determination and selection process comprising two steps. If the bit operator to achieve an absolute value, out determination step may be omitted, the operation result returned directly. Here to explain the rationale for using the absolute value of the site operator. We know: any number on a bit, and 0 XOR operation result of the operation are the same as the number on the bins. This conclusion about the extension, expanded from the original single a bit of a number to the number itself, the other two conclusions can be drawn, the first conclusion: any integer after XORed with 0 remains unchanged . If the small partner does not understand, see below, we digitally Examples 5 analysis to explain:
Java one thousand Q: Java-bit arithmetic classic application (a)
another conclusion: any integer by -1 XORed after adding 1, the result is the opposite of this number . If you do not understand it, or see the figure, still based on digital analysis to explain 5:
Java one thousand Q: Java-bit arithmetic classic application (a)
more than two conclusions, the first conclusion is easier to understand. We simply explain the principles of the second conclusion. The key can be established second conclusion is that, represented by a value of -1's complement code happens to be a full 32-bit binary string of 1's. This binary string with any of the other string of binary bitwise exclusive-or operation, can achieve "inverted" effect, and in accordance with the calculation rule of complement, together with a positive number bitwise after 1, it is obtained Opposite number. Figure 5 such as a digital, then bitwise, is obtained together with 1 -5.
So far, we already know how to get the opposite of a number of by-bit operation mode. In " the Java one thousand Q: Java language Bitwise Operators Comments"In a further spoken text: it must be a positive number of 0 through unsigned int type 31 right after, obtained while a negative sign to the right through the belt 31 is -1 is obtained. We can obtained by the 0 or -1 to the right, it is determined that the number is positive or negative. Known positive and negative attributes number, then use the resulting bit-wise operations this number itself or its opposite number, the absolute value of this number can be determined. According to this idea, we can write a program since the absolute value of the, programmed as follows:
Java one thousand Q: Java-bit arithmetic classic application (a)
the sample program is a variable of type int, long type if instead of a right shift with sign 63 may have the same operational effects .

(To be continued ...)
If you want to learn Java programming system, and welcome to my site in the video lessons.

Guess you like

Origin blog.51cto.com/2266836/2471460