Java.lang Package Classes

Java Integer - numberOfTrailingZeros() Method



The java.lang.Integer.numberOfTrailingZeros() method returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value. The method returns 32 if the specified value has no one-bits in its two's complement representation, in other words if it is equal to zero.

Syntax

public static int numberOfTrailingZeros(int i)

Parameters

i Specify the value whose number of trailing zeros is to be computed.

Return Value

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value, or 32 if the value is equal to zero.

Exception

NA.

Example:

In the example below, the java.lang.Integer.numberOfTrailingZeros() method returns the number of zero bits following the rightmost one-bit in the two's complement binary representation of the given int value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating an int value
    int x = 50; 

    //printing value of x, its binary 
    //representation and numberOfTrailingZeros
    System.out.println("The values of x is: " + x);
    System.out.print("The binary representation of x is: ");
    System.out.println(Integer.toBinaryString(x));
    System.out.print("The numberOfTrailingZeros is: ");
    System.out.println(Integer.numberOfTrailingZeros(x));
  }
}

The output of the above code will be:

The values of x is: 50
The binary representation of x is: 110010
The numberOfTrailingZeros is: 1

❮ Java.lang - Integer