Java bit Operation Classic example

Source: Internet
Author: User

A source code, anti-code, complement

Positive source code, anti-code, the same complement, for example 5:

5 Source: 101

5 Anti-code: 101

Complement of 5:101

Negative source code, anti-code, the complement of different, for example-5:

-5 of the source code: 10000101

-5 of the anti-code: 111111010 (Take the reverse operation)

-5 Complement: 111111011 (complement plus 1 operation)

All data in the computer is stored and computed in the complement.

Two-bit operation

Bit operations contain &,|,!, respectively, expressed with, or not.

eg

5 & 4 = 101 & 100 = 100 (bitwise take and, 1 & 1 = 1,0 & 1 = 0)

5 | 4 = 101 | 100 = 101 (bitwise take OR, 1 | 0 = 1, 0 | 0 = 0)

!5 =! 101 = 010 (bitwise "Inverse",!1 = 0,!0 = 1)

Three-bit Operation example

Eg: given a signed integer x (32-bit), write a method that checks if the number is a 4 N-square, and N is a non-negative integer.

Here's how to answer this question in Java:

public class Test {
public static void Main (string[] args) {
for (int i = -64; i <; i+=1) {
if (ISPOWEROFFOUR5 (i))
SYSTEM.OUT.PRINTLN ("Test" + i + "is power of four!");
}
}

public static Boolean isPowerOfFour1 (int x) {
if (x = = 0) return false;
while ((x% 4) = = 0) {
x/= 4;
}
return x = = 1;
}

public static Boolean ISPOWEROFFOUR2 (int x) {
if (x = = 0) return false;
while ((x% 4) = = 0) {
x >>= 2;
}
return x = = 1;
}

public static Boolean ISPOWEROFFOUR3 (int x) {
Double n = Math.log (x)/Math.log (4);
if (n-(int) n! = 0) return false;
return n >= 0;
}

public static Boolean isPowerOfFour4 (int x) {
if (x = = 0) return false;
int y = (int) math.sqrt (x);
if (y * y = = x)
Return ((Y & (y-1)) = = 0);
return false;
}

public static Boolean ISPOWEROFFOUR5 (int x) {
if (x = = 0) return false;
Return (x & (x-1)) = = 0 && (x & 0xAAAAAAAA) = = 0;
}
}

To explain a little bit, the algorithm complexity of the first method is log4x, the principle is, generally 4 of the N-square number (n is greater than or equal to 0 positive integer), the x has been taken, the final result must be equal to 1, such as 4 * 4 * 4 = 64.

The second method, like the first one, >>2 equivalent to/4.

The third method uses the formula: 4N = X = n = log4x = Logx/log4, so you only need to determine that n is greater than or equal to 0 and is an integer (if n (int) N! = 0 indicates that n is not an integer).

The fourth method has the following principles:

40 41 42 43 ... 4n

(22) 0 (22) 1 (22) 2 (22) 3 ... (n)

Then y = math.sqrt (x) equals:

20 21 22 23 ... 2n

So we just need to determine that Y is an integer, and y is 2 N, n is greater than or equal to 0 and is an integer:

To determine whether an integer y is 2 of the N-square, just Judge y& (y-1) and so on is not equal to 0.

The fifth method has the following principles:

40 1

41 100

42 10000

43 1000000

...

4n 100000000 ...

It can be found that all 2 of the 4-based n-ary numbers have only 1 bits of 1, and 1 of this one is on the odd digits.

Therefore, we first determine that X's 2 binary is only one, and that bit is not on the even digits, but also note that 0 must be excepted, because 0& any number equals 0.

As I said earlier, X & (x-1) = = 0 indicates that x is the n-th side of 2, and we should know that any 2 binary only has a number of 1 can be represented as the n-th square of 2.

Now that you have determined that X's 2 decimal number is only one 1, then (x & 0xAAAAAAAA) = = 0 indicates that this one is 1 on the odd bit, because 0xAAAAAAAA = 1010 1010 1010 1010

Java bit Operation Classic example

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.