The numberOfLeadingZeros() method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.
Syntax
public static int numberOfLeadingZeros (int i)
Parameters
The parameter ‘i’ represents a value whose number of leading zeros is to be determined.
Return Value
This method returns:
- The Number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation.
- 32 if the value is equal to zero.
Example 1
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerNumberOfLeadingZerosExample1 { public static void main(String[] args) { Integer val1=78; //returns the Number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation int i= Integer.numberOfLeadingZeros(val1); System.out.println("Number of leading zeros returned : "+i); } } |
Output
1 2 3 |
Number of leading zeros returned : 25 |
Example 2
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerNumberOfLeadingZerosExample2 { public static void main(String[] args) { Integer val1=0; //returns 32 if the value is equal to zero int i= Integer.numberOfLeadingZeros(val1); System.out.println("Number of leading zeros returned : "+i); } } |
Output
1 2 3 |
Number of leading zeros returned : 32 |
Example 3
1 2 3 4 5 6 7 8 9 10 |
public class JavaIntegerNumberOfLeadingZerosExample3 { public static void main(String[] args) { Integer val1=Integer.MIN_VALUE; //returns 0 if the value is equal to MAX_VALUE int i= Integer.numberOfLeadingZeros(val1); System.out.println("Number of leading zeros returned : "+i); } } |
Output
1 2 3 |
Number of leading zeros returned : 0 |