Related Articles

# Integer.numberOfTrailingZeros() Method in Java with Example

• Last Updated : 05 Dec, 2018

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 :

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

`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.

 `// 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));``     ` `}``}`
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.

 `// 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));``     ` `}``}`
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.

 `// 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``));``     ` `}``}`

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.

 `// 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"``));``}``}`

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