Integer.numberOfTrailingZeros() method in Java

numberOfTrailingZeros() method is a static method of the Integer wrapper class that counts the number of zero bits following the lowest-order ( “rightmost” ) one-bit in the 2’s complement binary representation of the specified number. In this post, we are going to look at the numberOfTrailingZeros() method in detail.

  • Method declaration – public static int numberOfTrailingZeros(int i)
  • What does it do? – It will count the number of zero bits following the lowest-order ( “rightmost” ) one-bit in the 2’s complement binary representation of the int value passed in the argument.
    • It will return 32 if the number passed in the argument has no 1-bit set in the 2’s complement of the binary representation, or we can say the number passed in the argument is 0
  • What does it return? It will return an integer denoting the count of the number of zero bits following the lowest-order ( “rightmost” ) one-bit in the 2’s complement binary representation of the int value passed in the argument.
Code Example
public class Codekru {

	public static void main(String[] args) {

		int i = 256;

		System.out.println("Binary representation of " + i + " : " + Integer.toBinaryString(i));
		System.out.println("No. of zero bits following the rightmost 1-bit in 2's complement representation: "
				+ Integer.numberOfTrailingZeros(i));

	}
}

Output –

Binary representation of 256 : 100000000
No. of zero bits following the rightmost 1-bit in 2's complement representation: 8
2's complement binary representation of 256

Here, the number of zero bits is counted from the rightmost 1-bit. As the 2’s complement binary representation of 256 is 100000000, so, the number of zero bits following the rightmost 1-bit is 8.

We have discussed how to use the numberOfTrailingZeros() method on positive integers and let’s now look at the negative integers as well.

Using numberOfTrailingZeros() method on negative integers

A positive integer can be directly represented by the binary strings but for a negative integer, we have to find the 2’s complement of that number. So, first, let’s see how we can calculate the 2’s complement of a negative number.

  • To find the 2’s complement of a number, we first have to know how many bits we are working with. In our case, the number of bits is 32
  • Let’s try to find the 2’s complement of the number -30
  • For this, we first have to write the binary representation of the number 30
0000 0000 0000 0000 0000 0000 0001 1110
  • Invert the binary digits
1111 1111 1111 1111 1111 1111 1110 0001
  • Now, add one to it
1111 1111 1111 1111 1111 1111 1110 0010

So, 2’s complement of -30 is “11111111111111111111111111100010”. Now, let’s see it with the program.

public class Codekru {

	public static void main(String[] args) {

		int i = -30;

		System.out.println("Binary representation of " + i + " : " + Integer.toBinaryString(i));
		System.out.println("No. of zero bits following the rightmost 1-bit in 2's complement representation: "
				+ Integer.numberOfTrailingZeros(i));

	}
}

Output –

Binary representation of -30 : 11111111111111111111111111100010
No. of zero bits following the rightmost 1-bit in 2's complement representation: 1
Q – What if we pass 0 into the numberOfTrailingZeros() argument?

Here, it will return 32 as there is no 1-bit in the binary representation of 0.

public class Codekru {

	public static void main(String[] args) {

		int i = 0;

		System.out.println("No. of zero bits following the rightmost 1-bit in 2's complement representation: "
				+ Integer.numberOfTrailingZeros(i));

	}
}

Output –

No. of zero bits following the rightmost 1-bit in 2's complement representation: 32

Just like numberOfTrailingZeros() is used to count the number of zero bits following the rightmost 1-bit in the 2’s complement representation, similarly, we also have the numberOfLeadingZeros() method that is used to count the number of zero bits preceding the leftmost 1-bit in the 2’s complement representation.

Please visit this link if you want to know more about the Integer wrapper class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *