Java Integer numberOfTrailingZeros() Method

Java numberofTrailingZeros() method is a part of the Integer class of the java.lang package. This method is used to return the zero bits following the lowest order one bit (rightmost) of the two's complement of the integer value passed as an argument. In other words, it converts int value to binary and returns a total number of zero bits following the lowest(rightmost) one bit.

If the passed value has no one bit i.e the value is zero then the value 32 is returned.

Syntax:

public static int numberOfTrailingZeros(int i)

Parameters:

The parameter passed is the integer value whose number of zero bits following the lowest order one bit is returned.

Returns:

Returns the number of zero bits that are following the lowest order one bit (rightmost) of the parameter passed.

Example 1:

Here, one positive number and one negative number is taken for a better understanding of the method.

The Integer.toBinaryString method is used to represent the equivalent binary form of the passed number.

public class StudyTonight
{
   public static void main(String[] args) 
   {
      int i = 170;   //positive number
      System.out.println("Original Number is " + i);
      System.out.println("Binary representation is = " + Integer.toBinaryString(i)); 
      System.out.println("Number of trailing zeros is " + Integer.numberOfTrailingZeros(i)); 
      //returns the number of zero bits following the lowest-order one bit
        
      int j=-57; //negative number
      System.out.println("Original Number is " + j);
      System.out.println("Binary representation is = " + Integer.toBinaryString(j)); 
      System.out.println("Number of trailing zeros is " + Integer.numberOfTrailingZeros(j)); 
      //returns the number of zero bits following the lowest-order one bit
   }
}


Original Number is 170
Binary representation is = 10101010
Number of trailing zeros is 1
Original Number is -57
Binary representation is = 11111111111111111111111111000111
Number of leading zeros is 0

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.*;
import java.util.Scanner;

public class StudyTonight
{
   public static void main(String[] args) 
   {
      try
      {
        System.out.println("Enter the number ");
        Scanner sc = new Scanner(System.in);
        int i=sc.nextInt();
        System.out.println("Binary representation is = " + Integer.toBinaryString(i)); 
        System.out.println("Number of trailing zeros is " + Integer.numberOfTrailingZeros(i)); 
        //returns the number of zero bits following the lowest-order one bit
      }
      catch(Exception e)
      {
        System.out.println("Invalid input");
      }
   }
}


Enter the number
87
Binary representation is = 1010111
Number of trailing zeros is 0
***********************************************
Enter the number
0x324
Invalid input

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.