# Java lang.Long.numberOfLeadingZeros() method in Java with Examples

java.lang.Long.numberOfLeadingZeros() is a built-in function in Java which returns the number of leading zero bits to the left of the highest order set bit. In simple terms, it returns the (64-position) where position refers to the highest order set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it returns 64.

Syntax:

```public static long numberOfLeadingZeros(long num)
Parameters:
num - the number passed
Returns:
the number of leading zeros before the highest-order set bit
```

Examples:

```Input : 8
Output : 60
Explanation: Binary representation of 8 is 1000
No of leading zeros to the left of the highest-order set
bit is 60.

Input : 25
Output : 59
```

The program below illustrates the java.lang.Long.numberOfLeadingZeros() function:

Program 1:

 `// Java program that demonstrates the ` `// Long.numberOfLeadingZeros() function ` ` `  `// include lang package ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``long` `l = ``8``; ` ` `  `        ``// returns the number of zero bits before the highest-order ` `        ``// set bit ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(l)); ` ` `  `        ``// second example ` `        ``l = ``25``; ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(l)); ` `    ``} ` `} `

Output:

```Number of leading zeros = 60
Number of leading zeros = 59
```

Note: In case of a negative number, every number will have 0 leading zeros.

Program 2: The program below demonstrates the use of function when a negative number is passed.

 `// Java program that demonstrates the ` `// Long.numberOfLeadingZeros() function ` `// negative number ` `// include lang package ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``long` `l = -``12``; ` ` `  `        ``// returns the number of zero bits before the highest-order ` `        ``// set bit ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(l)); ` ` `  `        ``l = -``100``; ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(l)); ` `    ``} ` `} `

Output:

```Number of leading zeros = 0
Number of leading zeros = 0
```

Program 3: It returns an error message when a decimal a string value is passed as an argument.

 `// Java program that demonstrates the ` `// Long.numberOfLeadingZeros() function ` `// decimal number ` `// include lang package ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// returns the number of zero bits before the highest-order ` `        ``// set bit ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(``10.45``)); ` `    ``} ` `} `

Output:

```prog.java:16: error: incompatible types: possible lossy conversion from double to long

Program 4: When a string value is passed in argument.

 `// Java program that demonstrates the ` `// Long.numberOfLeadingZeros() function ` `// string number ` `// include lang package ` `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// returns the number of zero bits before the highest-order ` `        ``// set bit ` `        ``System.out.println(``"Number of leading zeros = "` `                           ``+ Long.numberOfLeadingZeros(``"10"``)); ` `    ``} ` `} `

Output:

```prog.java:16: error: incompatible types: String cannot be converted to long

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : shubham_singh

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.