Integer.numberOfTrailingZeros() Method in Java with Example

The Java.lang.Integer.numberOfTrailingZeros() is the method which returns the total number of zero(0) bits following the lowest-order (ie.Rightmost or least significant “1” bit)one-bit in the two’s complement binary representation of the specified integer value or we can say that it is the function which converts int value to Binary then considers the lowest one bit and return no. of zero bits following it. If the specified integer value has no one-bits in its two’s complement representation, in other words if it is equal to zero then it will returns 32.

Syntax :

public static int numberOfTrailingZeros(int a)

Parameter: The parameter a is an integer value.

Return Value: This method returns the number of zero bits following the lowest-order one-bit or set-bit in the two’s complement binary representation of the specified int value, or 32 if the value is equal to zero.

Explanation

  • Consider an integer a = 170
  • Binary Representation = 10101010
  • Lowest one bit = 2
  • Number of trailing zeros = 1

Below programs illustrate the Java.lang.Integer.numberOfTrailingZeros() method.

Program 1: For a positive number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.numberOfTrailingZeros() method
import java.lang.*;
  
public class TrailingZeros {
  
public static void main(String[] args) {
  
    int a = 155;
    System.out.println("Integral Number = " + a);
  
  
    // Returns the number of zero bits following the lowest-order 
        //rightmost one-bit 
    System.out.print("Number of Trailing Zeros = ");
    System.out.println(Integer.numberOfTrailingZeros(a));
         a = 24;
    System.out.println("Integral Number = " + a);
  
  
    // Returns the number of zero bits following the lowest-order 
    //rightmost one-bit 
    System.out.print("Number of Trailing Zeros = ");
    System.out.println(Integer.numberOfTrailingZeros(a));
      
}
}

chevron_right


Output:

Integral Number = 155
Number of Trailing Zeros = 0
Integral Number = 24
Number of Trailing Zeros = 3

Note: Here the number of trailing zeros is equal to 0 for 155 ,this is because in the binary representation 10011011 there is no zero which is following the lowest-order (ie.Rightmost or least significant “1” bit)one-bit.

Program 2: For a negative number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.numberOfTrailingZeros() method
import java.lang.*;
  
public class TrailingZeros {
  
public static void main(String[] args) {
  
    int a = -1;
    System.out.println("Integral Number = " + a);
  
  
    // Returns the number of zero bits following the lowest-order 
    //rightmost one-bit 
    System.out.print("Number of Trailing Zeros = ");
    System.out.println(Integer.numberOfTrailingZeros(a));
        a = -90;
    System.out.println("Integral Number = " + a);
  
  
    // Returns the number of zero bits following the lowest-order 
    //rightmost one-bit 
    System.out.print("Number of Trailing Zeros = ");
    System.out.println(Integer.numberOfTrailingZeros(a));
      
}
}

chevron_right


Output:

Integral Number = -1
Number of Trailing Zeros = 0
Integral Number = -90
Number of Trailing Zeros = 1

Program 3: For a decimal value.
Note: It returns an error message when a decimal value is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.numberOfTrailingZeros() method
import java.lang.*;
  
public class TrailingZeros {
  
public static void main(String[] args) {
  
     System.out.println("Number of trailing zeros = "+
     Integer.numberOfTrailingZeros(12.66));
      
}
}

chevron_right


Output:

prog.java:10: error: incompatible types: possible lossy conversion from double to int
     Integer.numberOfTrailingZeros(12.66));
                                   ^
Note: Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error

Program 4: For a string value.
Note: It returns an error message when a string value is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// Java.lang.Integer.numberOfTrailingZeros() method
import java.lang.*;
  
public class TrailingZeros {
  
public static void main(String[] args) {
  
 System.out.println("Number of trailing zeros = "
                           + Integer.numberOfTrailingZeros("12"));
}
}

chevron_right


Output:

prog.java:10: error: incompatible types: String cannot be converted to int
                           + Integer.numberOfTrailingZeros("12"));
                                                           ^
Note: Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error


My Personal Notes arrow_drop_up

lets make it a lil simple

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.