In Java, Integer.highestOneBit(i) returns an int that has a single 1 bit, in the same position as the leftmost1 bit of i. All other bits are zero.
For a positive number the result is the largest power of two that is not bigger than it. For example, 100 is 1100100 in binary, and the result is 1000000, which is 64.
A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.highestOneBit(...). Integer lives in java.lang, so no import is needed.
Syntax
One argument: the int to examine.
Java
int a = Integer.highestOneBit(100); // 64
int b = Integer.highestOneBit(8); // 8
int c = Integer.highestOneBit(0); // 0
Call
What it does
Integer.highestOneBit(i)
The leftmost set bit of i, as a number.
Integer.highestOneBit(n - 1) << 1
Rounds nup to a power of two (for n above 1).
Integer.numberOfLeadingZeros(i)
Tells you where that top bit sits.
Integer.lowestOneBit(i)
The rightmost set bit instead.
For a positive number, highestOneBit rounds down to a power of two. For a negative number the answer is always Integer.MIN_VALUE, and for 0 it is 0.
Library declarations
Java
// java.lang.Integer
public static int highestOneBit(int i)
Parameters
Parameter
Description
i (int)
The number to examine.
Return Value
Return Value
Description
int
A value with a single bit set at the position of the highest set bit of i, or 0 if i is 0.
Powers of two map to themselves. 1 and 64 already have a single bit set.
Others round down. 10 becomes 8, 100 becomes 64, and 1000 becomes 512, the largest power of two not above the number.
Negative numbers. -1 has its top bit set, so the result is Integer.MIN_VALUE.
Example 2 — Pick a Power-of-Two Buffer Size
Round a requested size down and up to the nearest power of two.
Java
public class IntegerHighestOneBitSize {
static int roundUp(int n) {
if (n <= 1) {
return 1;
}
return Integer.highestOneBit(n - 1) << 1;
}
public static void main(String[] args) {
int[] requests = {1000, 1024, 1025, 3, 1};
for (int n : requests) {
System.out.println(n + ": down " + Integer.highestOneBit(n) + ", up " + roundUp(n));
}
}
}
Output
1000: down 512, up 1024
1024: down 1024, up 1024
1025: down 1024, up 2048
3: down 2, up 4
1: down 1, up 1
How It Works
Rounding down is direct.highestOneBit(1000) is 512.
Rounding up uses n - 1. The highest bit of 999 is 512, and shifting it left by one gives 1024.
Exact powers stay put. For 1024, the value 1023 has highest bit 512, so shifting gives 1024 again, and 1025 moves up to 2048.
Small inputs. 3 rounds down to 2 and up to 4, and 1 stays 1 because the helper handles it first.
Example 3 — Find the Position of the Top Bit
Show the top bit in binary and its position, and compare with the full binary form of the number.
Java
public class IntegerHighestOneBitPosition {
public static void main(String[] args) {
int[] numbers = {37, 255, 256, 1000};
for (int n : numbers) {
int top = Integer.highestOneBit(n);
int position = Integer.numberOfTrailingZeros(top);
System.out.println(n + " = " + Integer.toBinaryString(n));
System.out.println(" top bit " + Integer.toBinaryString(top)
+ " (value " + top + ", position " + position + ")");
}
}
}
Output
37 = 100101
top bit 100000 (value 32, position 5)
255 = 11111111
top bit 10000000 (value 128, position 7)
256 = 100000000
top bit 100000000 (value 256, position 8)
1000 = 1111101000
top bit 1000000000 (value 512, position 9)
How It Works
Two views. Each number is printed in full, then its top bit alone.
A one followed by zeros. The result is always a single 1 followed by zeros: 37 (100101) gives 100000, which is 32.
Position counts from zero.numberOfTrailingZeros(top) counts the zeros after the bit, so 32 is at position 5.
The boundary. 255 has a top bit at position 7, but 256 jumps to position 8.
Frequently Asked Questions
It keeps only the leftmost 1 bit of a number. For a positive int this is the largest power of two that does not exceed it.
Use Integer.highestOneBit(n). For example, highestOneBit(1000) returns 512.
Use Integer.highestOneBit(n - 1) << 1 for n above 1. For 1000 this gives 1024.
It returns 0 for 0 and Integer.MIN_VALUE for any negative number, because the top bit is set.
Use 31 - Integer.numberOfLeadingZeros(n), which gives the zero-based position.