Java Integer numberOfLeadingZeros() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.numberOfLeadingZeros(i) returns how many 0 bits come before the first 1 bit, counting from the left of the 32-bit form of i.

The number 1 has 31 leading zeros, because only the last of its 32 bits is set. The number 0 has no 1 bit at all, so its answer is 32, and every negative number has 0 leading zeros because its top bit is already 1.

A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.numberOfLeadingZeros(...). Integer lives in java.lang, so no import is needed.

Syntax

One argument: the int to examine.

Java
int a = Integer.numberOfLeadingZeros(1);     // 31
int b = Integer.numberOfLeadingZeros(255);   // 24
int c = Integer.numberOfLeadingZeros(-1);    // 0
CallWhat it does
Integer.numberOfLeadingZeros(i)Zero bits before the first 1, from 0 to 32.
32 - Integer.numberOfLeadingZeros(i)The number of binary digits needed (the bit length).
31 - Integer.numberOfLeadingZeros(i)The position of the top bit, which is also the floor of log base 2.
Integer.numberOfTrailingZeros(i)Counts zeros from the right instead.

The answer is 32 for 0 and 0 for any negative number. Use it with care in a formula, because both are special cases.

Library declarations
Java
// java.lang.Integer
public static int numberOfLeadingZeros(int i)

Parameters

ParameterDescription
i (int)The number to examine.

Return Value

Return ValueDescription
intThe number of zero bits in front of the highest set bit: 0 for negative numbers, up to 31 for 1, and 32 for 0.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
numberOfTrailingZeros(i)Counts zeros at the right end instead.
highestOneBit(i)Gives the value of the top set bit.
bitCount(i)Counts the ones rather than the zeros.
toBinaryString(i)Drops leading zeros; its length is the bit length.

Java Examples

Example 1 — Count the Leading Zeros of Several Numbers

Print the leading zeros of a range of values, including 0 and a negative number.

Java
public class IntegerLeadingZerosDemo {
    public static void main(String[] args) {
        int[] numbers = {0, 1, 2, 255, 256, Integer.MAX_VALUE, -1};

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

How It Works

  1. 1 is far to the right. Only the last bit is set, so 31 zeros come first.
  2. Each doubling takes one away. 2 has 30 leading zeros, and 255 (eight ones) has 24.
  3. 256 needs a ninth bit. It has one more bit than 255, so it has one fewer leading zero: 23.
  4. The edges. MAX_VALUE has just the sign bit unset, so 1 leading zero; -1 has 0; and the number 0 has no 1 bit, so the answer is 32.

Example 2 — Work Out How Many Bits a Number Needs

Use 32 - numberOfLeadingZeros(n) as a bit length, and check it against the length of the binary text.

Java
public class IntegerLeadingZerosLength {
    public static void main(String[] args) {
        int[] numbers = {1, 5, 255, 256, 1000};

        for (int n : numbers) {
            int bits = 32 - Integer.numberOfLeadingZeros(n);
            System.out.println(n + " needs " + bits + " bits, text length "
                    + Integer.toBinaryString(n).length());
        }
    }
}

How It Works

  1. 32 minus the zeros. Leading zeros are wasted space, so what is left is the number of bits that matter.
  2. It matches the text. toBinaryString drops leading zeros, so its length gives the same number.
  3. Small and large. 5 (101) needs 3 bits, 255 needs 8, and 256 needs 9.
  4. Thousands. 1000 is 1111101000, ten bits.

Example 3 — Find the Floor of log2

Use 31 - numberOfLeadingZeros(n) to find the highest set bit, and guard the special case of 0.

Java
public class IntegerLeadingZerosLog {
    static int floorLog2(int n) {
        if (n <= 0) {
            return -1;
        }
        return 31 - Integer.numberOfLeadingZeros(n);
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 8, 1000, 1024, 0};

        for (int n : numbers) {
            System.out.println("floor(log2(" + n + ")) = " + floorLog2(n));
        }
    }
}

How It Works

  1. Position of the top bit. The highest set bit of a positive number is at position 31 - leadingZeros, counting from 0.
  2. Powers of two. 1 gives 0, 2 gives 1, 8 gives 3, and 1024 gives 10.
  3. In between. 3 and 1000 round down: 3 is still below 4, so the answer is 1, and 1000 is below 1024, so the answer is 9.
  4. No answer for 0. The helper returns -1 for zero or negative input, because a zero has no top bit.

Frequently Asked Questions

It counts the 0 bits before the first 1 bit in the 32-bit form of an int. For 1 the answer is 31.
It returns 32, because there is no 1 bit. For negative numbers it returns 0.
Use 32 - Integer.numberOfLeadingZeros(n) for a positive n. It equals the length of Integer.toBinaryString(n).
Use 31 - Integer.numberOfLeadingZeros(n) for n above 0.
Yes. On most processors it compiles to a single instruction, so it is faster than a loop.

Next: numberOfTrailingZeros()

Count the zero bits after the last 1 bit.

numberOfTrailingZeros() 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.

7 people found this page helpful