Java Long numberOfTrailingZeros Method

JavaJavaBeginner
Practice Now

Introduction

The numberOfTrailingZeros() method returns the number of zero bits following the lowest order one bit (rightmost) of the passed long value. This method is a part of the Long class of the java.lang package.

Create a new Java file

Create a new Java file using the nano editor by typing the following command in your terminal:

touch ~/project/TrailingZeros.java

Write Java code

Add the following code to use the numberOfTrailingZeros() method to get the number of trailing zeros in a long value:

public class TrailingZeros {
    public static void main(String[] args) {
        // positive number
        long num1 = 170L;
        System.out.println("Original Number is " + num1);
        System.out.println("Binary representation is = " + Long.toBinaryString(num1));
        System.out.println("Number of trailing zeros is = " + Long.numberOfTrailingZeros(num1));

        // negative number
        long num2 = -57L;
        System.out.println("Original Number is " + num2);
        System.out.println("Binary representation is = " + Long.toBinaryString(num2));
        System.out.println("Number of trailing zeros is " + Long.numberOfTrailingZeros(num2));
    }
}

Compile and run the code

Compile the Java code using the following command in your terminal:

javac ~/project/TrailingZeros.java

After the compilation is complete, run the program by typing the following command in your terminal:

java TrailingZeros

The output of the program will look like this:

Original Number is 170
Binary representation is = 10101010
Number of trailing zeros is = 1
Original Number is -57
Binary representation is = 1111111111111111111111111111111111111111111111111111111111000111
Number of trailing zeros is 0

User-defined input

You can also take input from the user and return the number of trailing zeros of the same. Add the following code to achieve the above behavior:

import java.util.Scanner;

public class TrailingZeros {
    public static void main(String[] args) {
        try {
            System.out.println("Enter a number: ");
            Scanner sc = new Scanner(System.in);
            long num3 = sc.nextLong();
            System.out.println("Binary representation is = " + Long.toBinaryString(num3));
            System.out.println("Number of trailing zeros is " + Long.numberOfTrailingZeros(num3));
        } catch (Exception e) {
            System.out.println("Invalid input");
        }
    }
}

Compile and run the updated code

Compile the updated Java code using the following command in your terminal:

javac ~/project/TrailingZeros.java

After the compilation is complete, run the updated program by typing the following command in your terminal:

java TrailingZeros

When the program prompts you for input, enter a long value of your choice and press the 'Enter' key. The program will display the binary representation of the number and the number of trailing zeros.

Summary

In this lab, you learned how to use the numberOfTrailingZeros() method to return the number of trailing zeros in a passed long value. You also created a user-defined input program to take input from the user and return the number of trailing zeros of the same.

Other Java Tutorials you may like