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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/classes_objects -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/class_methods -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/exceptions -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/modifiers -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/oop -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/packages_api -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/user_input -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/wrapper_classes -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/identifier -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/arrays -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/comments -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/data_types -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/operators -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/output -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/strings -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/variables -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} java/system_methods -.-> lab-117896{{"`Java Long numberOfTrailingZeros Method`"}} end

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