Signup/Sign In
LAST UPDATED: AUGUST 27, 2020

Java Integer numberOfLeadingZeros() Method

Java numberofLeadingZeros() method is a part of the Integer class of the java.lang package. This method is used to return the zero bits that are preceding the highest order one bit (leftmost) of the two's complement of the integer value passed as an argument. In other words, it converts int value to binary and returns a total number of zero bits preceding the highest(leftmost) one bit.

If the passed value has no one bit i.e the value is zero then the value 32 is returned.

Syntax:

public static int numberOfLeadingZeros(int i)

Parameters:

The parameter passed is the integer value whose two's complement zero bits preceding the highest order one bit is returned.

Returns:

Returns the zero bits that are preceding the highest order one bit (leftmost) of the two's complement of the parameter.

Example 1:

Here, one positive number and one negative number is taken for a better understanding of the method.

The Integer.toBinaryString method is used to represent the equivalent binary form of the passed number.

import java.lang.*;

public class StudyTonight
{

   public static void main(String[] args) 
   {

      int i = 170;   //positive number
      System.out.println("Original Number is " + i);
      System.out.println("Binary representation is = " + Integer.toBinaryString(i)); // converting into binary string of base 2 
      System.out.println("Number of leading zeros is " + Integer.numberOfLeadingZeros(i)); //returns the number of zero bits preceding the highest-order one bit
        
      int j=-57; //negative number
      System.out.println("Original Number is " + j);
      System.out.println("Binary representation is = " + Integer.toBinaryString(j)); // converting into binary string of base 2 
      System.out.println("Number of leading zeros is " + Integer.numberOfLeadingZeros(j)); //returns the number of zero bits preceding the highest-order one bit
   }
}


Original Number is 170
Binary representation is = 10101010
Number of leading zeros is 24
Original Number is -57
Binary representation is = 11111111111111111111111111000111
Number of leading zeros is 0

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.*;
import java.util.Scanner;

public class StudyTonight
{

   public static void main(String[] args) 
   {
      try
      {
        System.out.println("Enter the number ");
        Scanner sc = new Scanner(System.in);
        int i=sc.nextInt();
        System.out.println("Binary representation is = " + Integer.toBinaryString(i)); // converting into binary string of base 2 
        System.out.println("Number of leading zeros is " + Integer.numberOfLeadingZeros(i)); //returns the number of zero bits preceding the highest-order one bit
      }
      catch(Exception e)
      {
        System.out.println("Invalid input");
      }
   }
}


Enter the number
67
Binary representation is = 1000011
Number of leading zeros is 25
**********************************************
Enter the number
0x779
Invalid input

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.