# Integer.numberOfLeadingZeros() Method in Java With Example

The java.lang.Integer.numberOfLeadingZeros() is the method which returns the total number of zero(0) bits preceding the highest-order (ie.leftmost or most significant “1” bit)one-bit in the two’s complement binary representation of the specified integer value or we can say that it is the function which converts int value to Binary then considers the highest one bit and return no. of zero bits preceding it. If the specified integer value has no one-bits in its two’s complement representation, in other words if it is equal to zero then it will returns 32.

Syntax :

`public static int numberOfLeadingZeros(int arg)`

Parameter : This method accepts a single parameter arg which is the integer value.

Return Value : This method returns the number of zero bits preceding the highest-order set-bit in the two’s complement binary representation of the specified int value, or 32 if the value is equal to zero.

Explanation:

• Consider any integer arg = 19
• Binary Representation = 0001 0011
• Highest bit(at 5) i.e.0001 0000
• so result = 16-5 i.e. 11

Below programs illustrate the java.lang.Integer.numberOfLeadingZeros() method.

Program 1: For a positive number.

 `// Java praogram to illustrate the ` `// java.lang.Integer.numberOfLeadingZeros() ` `import` `java.lang.*; ` ` `  `public` `class` `LeadingZeros { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``int` `a = ``155``; ` `        ``System.out.println(``"Integral Number = "` `+ a); ` ` `  `        ``// Returns the number of zero bits preceding the highest-order ` `        ``// leftmost one-bit ` `        ``System.out.print(``"Number of Leading Zeros = "``); ` `        ``System.out.println(Integer.numberOfLeadingZeros(a)); ` `        ``a = ``10``; ` `        ``System.out.println(``"Integral Number = "` `+ a); ` ` `  `        ``// Returns the number of zero bits preceding the highest-order ` `        ``// leftmost one-bit ` `        ``System.out.print(``"Number of Leading Zeros = "``); ` `        ``System.out.println(Integer.numberOfLeadingZeros(a)); ` `    ``} ` `} `

Output:

```Integral Number = 155
Number of Leading Zeros = 24
Integral Number = 10
Number of Leading Zeros = 28
```

Program 2: For a negative number.

 `// Java praogram to illustrate the ` `// java.lang.Integer.numberOfLeadingZeros() ` `import` `java.lang.*; ` ` `  `public` `class` `LeadingZeros { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``int` `a = -``15``; ` `        ``System.out.println(``"Number = "` `+ a); ` ` `  `        ``// Returns the number of zero bits preceding the highest-order ` `        ``// leftmost one-bit ` `        ``System.out.print(``"Number of Leading Zeros = "``); ` `        ``System.out.println(Integer.numberOfLeadingZeros(a)); ` `    ``} ` `} `

Output:

```Number = -15
Number of Leading Zeros = 0
```

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

 `// Java praogram to illustrate the ` `// java.lang.Integer.numberOfLeadingZeros() ` `import` `java.lang.*; ` ` `  `public` `class` `LeadingZeros { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Returns the number of zero bits preceding the highest-order ` `        ``// leftmost one-bit ` `        ``System.out.print(``"Number of Leading Zeros = "``); ` `        ``System.out.println(Integer.numberOfLeadingZeros(``16.32``)); ` `    ``} ` `} `

Output:

```prog.java:11: error: incompatible types: possible lossy conversion from double to int
^
Note: Some messages have been simplified; recompile with
-Xdiags:verbose to get full output
1 error```

Program 4: For a string value is passed in argument.
Note:It returns an error message when a string is passed as an argument.

 `// Java praogram to illustrate the ` `// java.lang.Integer.numberOfLeadingZeros() ` `import` `java.lang.*; ` ` `  `public` `class` `LeadingZeros { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// returns the number of zero bits preceding the highest-order ` `        ``// leftmost one-bit ` `        ``System.out.print(``"Number of Leading Zeros = "``); ` `        ``System.out.println(Integer.numberOfLeadingZeros(``"18"``)); ` `    ``} ` `} `

Output:

```prog.java:11: error: incompatible types: String cannot be converted to int
^
Note: Some messages have been simplified; recompile with
-Xdiags:verbose to get full output
1 error```

My Personal Notes arrow_drop_up lets make it a lil simple

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.

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.