Java Integer highestOneBit() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.highestOneBit(i) returns an int that has a single 1 bit, in the same position as the leftmost 1 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
CallWhat it does
Integer.highestOneBit(i)The leftmost set bit of i, as a number.
Integer.highestOneBit(n - 1) << 1Rounds n up 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

ParameterDescription
i (int)The number to examine.

Return Value

Return ValueDescription
intA value with a single bit set at the position of the highest set bit of i, or 0 if i is 0.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
lowestOneBit(i)Isolates the rightmost set bit instead.
numberOfLeadingZeros(i)Counts zeros in front of the highest bit.
bitCount(i)Counts all set bits, not just the top one.
toBinaryString(i)Shows the result as a 1 followed by zeros.

Java Examples

Example 1 — Round Numbers Down to a Power of Two

Print the highest set bit for a range of values.

Java
public class IntegerHighestOneBitDemo {
    public static void main(String[] args) {
        int[] numbers = {0, 1, 10, 64, 100, 1000, -1};

        for (int n : numbers) {
            System.out.println(n + " -> " + Integer.highestOneBit(n));
        }
    }
}

How It Works

  1. Zero stays zero. There is no set bit to keep.
  2. Powers of two map to themselves. 1 and 64 already have a single bit set.
  3. Others round down. 10 becomes 8, 100 becomes 64, and 1000 becomes 512, the largest power of two not above the number.
  4. 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));
        }
    }
}

How It Works

  1. Rounding down is direct. highestOneBit(1000) is 512.
  2. Rounding up uses n - 1. The highest bit of 999 is 512, and shifting it left by one gives 1024.
  3. Exact powers stay put. For 1024, the value 1023 has highest bit 512, so shifting gives 1024 again, and 1025 moves up to 2048.
  4. 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 + ")");
        }
    }
}

How It Works

  1. Two views. Each number is printed in full, then its top bit alone.
  2. A one followed by zeros. The result is always a single 1 followed by zeros: 37 (100101) gives 100000, which is 32.
  3. Position counts from zero. numberOfTrailingZeros(top) counts the zeros after the bit, so 32 is at position 5.
  4. 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.

Next: intValue()

Unwrap an Integer into a primitive int.

intValue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful