Test the Live Example
Now that you know how NumberOfTrailingZeros()
method works, you can test out a live example to see if you can produce similar output.
import java.lang.*;
public class NumberOfTrailingZerosDemo {
public static void main(String[] args) {
int i = 500; //2 trailing zeros
System.out.println("Number is like : "+ i);
System.out.println("Binary representation is = " + Integer.toBinaryString(i));
System.out.println("Number of trailing zeros is " + Integer.numberOfTrailingZeros(i));
int j = 456; //2 trailing zeros
System.out.println("Number is like : "+ j);
System.out.println("Binary representation is = " + Integer.toBinaryString(j));
System.out.println("Number of trailing zeros is " + Integer.numberOfTrailingZeros(j));
int k = -8; //3 leading zeros
System.out.println("Number is like : "+ k);
System.out.println("Binary representation is = " + Integer.toBinaryString(k));
System.out.println("Number of zeros behind the rightmost 1-bit is " + Integer.numberOfTrailingZeros(k));
}
}
Execute the above code in the terminal to get the output.
javac NumberOfTrailingZerosDemo.java
java NumberOfTrailingZerosDemo
which will produce the following output.
Number is like : 500
Binary representation is = 111110100
Number of trailing zeros is 2
Number is like : 456
Binary representation is = 111001000
Number of trailing zeros is 2
Number is like : -8
Binary representation is = 11111111111111111111111111111000
Number of zeros behind the rightmost 1-bit is 3