Java Integer numberOfTrailingZeros() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.numberOfTrailingZeros(i) returns how many 0 bits come after the last 1 bit, counting from the right of the 32-bit form of i.

In everyday terms it tells you how many times a number can be divided by 2 and stay whole. For 8, which is 1000, the answer is 3. For an odd number the answer is 0, and for the number 0 itself it is 32.

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

Syntax

One argument: the int to examine.

Java
int a = Integer.numberOfTrailingZeros(8);     // 3  (1000)
int b = Integer.numberOfTrailingZeros(12);    // 2  (1100)
int c = Integer.numberOfTrailingZeros(0);     // 32
CallWhat it does
Integer.numberOfTrailingZeros(i)Zero bits after the last 1, from 0 to 32.
n >> Integer.numberOfTrailingZeros(n)Strips every factor of 2 from n, leaving an odd number.
Integer.numberOfTrailingZeros(mask)The index of the first flag that is set in a bit mask.
Integer.numberOfLeadingZeros(i)Counts zeros from the left instead.

For 0 the answer is 32, so check for 0 before using the result as a bit position.

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

Parameters

ParameterDescription
i (int)The number to examine.

Return Value

Return ValueDescription
intThe number of zero bits after the lowest set bit, from 0 up to 31, or 32 if i is 0.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
numberOfLeadingZeros(i)Counts zeros from the left end.
lowestOneBit(i)Gives the value of the lowest set bit instead of its position.
bitCount(i)Counts all the ones.
highestOneBit(i)The leftmost set bit.

Java Examples

Example 1 — Count the Trailing Zeros of Several Numbers

Print the trailing zeros for several numbers, including 0 and negatives.

Java
public class IntegerTrailingZerosDemo {
    public static void main(String[] args) {
        int[] numbers = {1, 8, 12, 40, 0, -8, Integer.MIN_VALUE};

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

How It Works

  1. Odd numbers have none. 1 ends in a 1 bit, so the answer is 0.
  2. Zeros after the last one. 8 (1000) has 3, 12 (1100) has 2, and 40 (101000) has 3.
  3. Zero. There is no 1 bit, so all 32 bits are trailing zeros.
  4. Negative numbers. -8 ends in the same 000 as 8, and MIN_VALUE is a single 1 followed by 31 zeros.

Example 2 — Count How Often a Number Halves Evenly

Use the trailing zeros to split a number into a power of two times an odd number.

Java
public class IntegerTrailingZerosHalve {
    public static void main(String[] args) {
        int[] numbers = {40, 96, 7, 1024};

        for (int n : numbers) {
            int twos = Integer.numberOfTrailingZeros(n);
            int odd = n >> twos;
            System.out.println(n + " = 2^" + twos + " * " + odd);
        }
    }
}

How It Works

  1. Trailing zeros are factors of 2. Every zero at the right end means one more division by 2 that stays whole.
  2. Shift out the zeros. n >> twos divides by 2 exactly that many times and leaves an odd number.
  3. 40 and 96. 40 is 8 × 5 and 96 is 32 × 3, so the odd parts are 5 and 3.
  4. Edge cases. 7 is odd already (20 × 7), and 1024 is a pure power of two, so its odd part is 1.

Example 3 — Find the First Set Flag in a Bit Mask

Find the lowest flag that is turned on, and handle the empty mask.

Java
public class IntegerTrailingZerosFlags {
    static String firstFlag(int mask) {
        if (mask == 0) {
            return "no flags set";
        }
        return "first flag is #" + Integer.numberOfTrailingZeros(mask);
    }

    public static void main(String[] args) {
        int[] masks = {40, 1, 0b10000, 0, 0b1101000};

        for (int mask : masks) {
            System.out.println(Integer.toBinaryString(mask) + ": " + firstFlag(mask));
        }
    }
}

How It Works

  1. Flags are bit positions. Flag #0 is the rightmost bit, flag #1 the next, and so on.
  2. The lowest set bit wins. 40 is 101000, which has 3 zeros at the right, so the first flag is #3.
  3. Binary literals. 0b10000 and 0b1101000 are just numbers written in binary; their first flags are #4 and #3.
  4. The empty mask. For 0 the method returns 32, which is not a real flag, so the helper checks for 0 first.

Frequently Asked Questions

It counts the 0 bits after the last 1 bit. For 8 (1000) the answer is 3.
32, because there is no 1 bit at all. Check for 0 first when you use the result as a position.
Use Integer.numberOfTrailingZeros(n). For 40 the answer is 3, because 40 is 8 times 5.
Use Integer.numberOfTrailingZeros(mask) for a mask that is not 0.
Yes, Long.numberOfTrailingZeros works the same way on a 64-bit value and returns 64 for 0.

Next: parseInt()

Convert a String of digits into an int.

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

8 people found this page helpful