Dark Mode On/Off

Java `numberofLeadingZeros() `method is a part of the `Long` 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 long value passed as an argument. In other words, it converts the long 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 64 is returned.

### Syntax:

``public static int numberOfLeadingZeros(long i)``

### Parameters:

The parameter passed is the long 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 `Long.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)
{

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

long j=-57L; //negative number
System.out.println("Original Number is " + j);
System.out.println("Binary representation is = " + Long.toBinaryString(j)); // converting into binary string of base 2
System.out.println("Number of leading zeros is " + Long.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 56
Original Number is -57
Binary representation is = 1111111111111111111111111111111111111111111111111111111111000111
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);
long i=sc.nextLong();
System.out.println("Binary representation is = " + Long.toBinaryString(i)); // converting into binary string of base 2
System.out.println("Number of leading zeros is " + Long.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
566
Binary representation is = 1000110110
Number of leading zeros is 54
*************************************************
Enter the number
-544
Binary representation is = 1111111111111111111111111111111111111111111111111111110111100000
Number of leading zeros is 0
************************************************
Enter the number
0x454
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.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.