Signup/Sign In

Java Long numberOfTrailingZeros() Method

Java numberofTrailingZeros() method is a part of the Long 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 long value passed as an argument. In other words, it converts the long 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 64 is returned.

Syntax:

public static int numberOfTrailingZeros(long i)

Parameters:

The parameter passed is the long 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 Long.toBinaryString method is used to represent the equivalent binary form of the passed number.

public class StudyTonight
{

   public static void main(String[] args) 
   {

      long i = 170L;   //positive number
      System.out.println("Original Number is " + i);
      System.out.println("Binary representation is = " + Long.toBinaryString(i)); // converting into binary string of base 2 
      System.out.println("Number of trailing zeros is = " + Long.numberOfTrailingZeros(i)); //returns the number of zero bits following the lowest-order one bit
        
      long j=-57L; //negative number
      System.out.println("Original Number is " + j);
      System.out.println("Binary representation is = " + Long.toBinaryString(j)); // converting into binary string of base 2 
      System.out.println("Number of trailing zeros is " + Long.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 = 1111111111111111111111111111111111111111111111111111111111000111
Number of trailing 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);
        long i=sc.nextLong();
        System.out.println("Binary representation is = " + Long.toBinaryString(i)); // converting into binary string of base 2 
        System.out.println("Number of trailing zeros is " + Long.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
77
Binary representation is = 1001101
Number of trailing zeros is 0
*******************************************
Enter the number
-343
Binary representation is = 1111111111111111111111111111111111111111111111111111111010101001
Number of trailing zeros is 0
***************************************************
Enter the number
0x57
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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.