Action with the underlying Integer.highestOneBit (int i) implementation of the method

There Integer class in such a way, you can give it a pass number, it returns the number of a power of less than or equal this number a 2. This method is highestOneBit (int i).

Enter the following example Demo, attention and method return value:

System.out.println(Integer.highestOneBit(15));  // 输出8
System.out.println(Integer.highestOneBit(16));  // 输出16
System.out.println(Integer.highestOneBit(17));  // 输出16
复制代码

This method implementation code amount is very small:

public static int highestOneBit(int i) {
	// HD, Figure 3-1
	i |= (i >>  1);
	i |= (i >>  2);
	i |= (i >>  4);
	i |= (i >>  8);
	i |= (i >> 16);
	return i - (i >>> 1);
}
复制代码

Next, we analyze in detail the logic of this code.

First, the function of this method: given a number, the number to find a power of less than or equal to the number 2 a.

If we want to achieve their own words, we need to know: how to determine a digital number is a power of 2.

Honestly, I can not think of what a good way to judge only can think of is if a digital convert it into a binary representation, then it will have a rule: If a number is the number of a power of 2, then its corresponding just a bit on the binary representation bit is 1, the other bits are all 0 bit. For example: 6 decimal, binary notation is: 0000 0110 8 decimal, binary notation is: 0000 1000 9 decimal, represented in binary as: 00001001 Therefore, we can use a number of binary representation to determine this number is not a power of 2 number. The key code is how to achieve it? Go through each bit position? Yes, but not how to do that? We still go back a closer look at how to achieve Integer is right?

public static int highestOneBit(int i) {
	// HD, Figure 3-1
	i |= (i >>  1);
	i |= (i >>  2);
	i |= (i >>  4);
	i |= (i >>  8);
	i |= (i >> 16);
	return i - (i >>> 1);
}
复制代码

We found that this code does not have any traversal, only bits and a subtraction operation, which means it's the realization of ideas and our own ideas to achieve completely different, its idea is: given a number, through a series of operations to give a number less than or equal to a power of the number 2 a.

That is: Given a numeral 18, through the calculation, to give 16.

18 is expressed in binary is: 00010010

The results (16) want to get is: 0001 0000

Then the calculation process is nothing more than the binary number 18 corresponds to the other bit bits except the most significant bit 1 are cleared, then we get the results we want.

Then how to achieve it through this process-bit computing?

We take 18 binary numbers corresponding 0001 0010to the example on the line: first 0001 0010right by 1 bit, to give 0000 1001, then ORed with itself: obtained 0001 1011.

Then the 0001 1011right by 2 bits, obtain 0000 0110, and then ORed with itself: obtained 0001 1111.

Then the 0001 1111right 4, to give 0000 0001, then ORed with itself: obtained 0001 1111.

Then 0001 1111right shifting 8, to obtain 0000 0000, and then ORed with itself: obtained 0001 1111.

Then the 0001 1111right 16, to give 0000 0000, then ORed with itself: obtained 0001 1111.

Then 0001 1111unsigned right by 1 bit, to give 0000 1111.

About unsigned right, you can read my previous articles written.

Finally 0001 1111 - 0000 1111 = 0001 0000shocked! Get the results we want.

In fact, this process can be abstracted into this: now there is a binary data, 0001****we do not care about the low value of the case, and we were right or its operation.

First 0001****right by 1 bit, to give 00001***, then ORed with itself: obtained 00011***.

Then the 00011***right by 2 bits, obtain 0000011*, and then ORed with itself: obtained 0001111*.

Then the 0001111*right 4, to give 00000001, then ORed with itself: obtained 00011111.

Do not forget the back again and again, here we can actually find a pattern: the purpose of the right or the operation is to let a certain number of turns 1 are low, then the result of the result of subtracting the result right one , the equivalent of clearing the original numbers low. That got the result we want.

This can only sigh for the use of JDK bit computing has reached the acme of perfection.

If you want to learn more about the wonderful knowledge of Java, distributed, micro-services, please pay attention to micro-channel public number: 1:25

reny125.jpeg

Guess you like

Origin juejin.im/post/5d679f67f265da03d55e6ab5