Java lang.Long.numberOfTrailingZeros() method in Java with Examples


java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it returns 64.

Syntax:

public static long numberOfTrailingZeros(long num)
Parameters:
num - the number passed 
Returns:
the number of trailing zeros after the lowest-order set bit

Examples:



Input :  8
Output : 3 
Explanation: Binary representation of 8 is 1000 
No of trailing zeros to the right of the lowest-order set
bit is 3. 

Input : 191
Output : 0 

The program below illustrates the java.lang.Long.numberOfTrailingZeros() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the
// Long.numberOfTrailingZeros() function
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        long l = 8;
  
        // returns the number of zero bits following the lowest-order
        // set bit
        System.out.println("Number of trailing zeros = "
                           + Long.numberOfTrailingZeros(l));
  
        // second example
        l = 25;
        System.out.println("Number of trailing zeros = "
                           + Long.numberOfTrailingZeros(l));
    }
}

chevron_right


Output:

Number of trailing zeros = 3
Number of trailing zeros = 0

Program 2: The program below demonstrates the use of function when a negative number is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the
// Long.numberOfTrailingZeros() function
// negative number
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        long l = -12;
  
        // returns the number of zero bits following the lowest-order
        // set bit
        System.out.println("Number of trailing zeros = "
                           + Long.numberOfTrailingZeros(l));
    }
}

chevron_right


Output:

Number of trailing zeros = 2

It returns an error message when a decimal a string value is passed as an argument.
Program 3: When a decimal value is passed in argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the
// Long.numberOfTrailingZeros() function
// decimal value as an argument
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // returns the number of zero bits following the lowest-order
        // set bit
        System.out.println("Number of trailing zeros = "
                           + Long.numberOfTrailingZeros(12.34));
    }
}

chevron_right


Output:

prog.java:16: error: incompatible types: possible lossy conversion from double to long
      System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros(12.34));

Program 4: When a string value is passed in argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program that demonstrates the
// Long.numberOfTrailingZeros() function
// string value as an argument
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // returns the number of zero bits following the lowest-order
        // set bit
        System.out.println("Number of trailing zeros = "
                           + Long.numberOfTrailingZeros("100"));
    }
}

chevron_right


Output:

prog.java:15: error: incompatible types: String cannot be converted to long
      System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros("100"));


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh