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)); } } |
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)); } } |
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
System.out.println(Integer.numberOfLeadingZeros(16.32));
^
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
System.out.println(Integer.numberOfLeadingZeros("18"));
^
Note: Some messages have been simplified; recompile with
-Xdiags:verbose to get full output
1 error
Recommended Posts:
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.util.ArrayList.addall() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java.lang.string.replace() method in Java
- Java.math.BigInteger.modInverse() method in Java
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.


