Java Integer numberOfLeadingZeros() method
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.
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
Number of leading zeros returned : 25Example 2
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
Number of leading zeros returned : 32Example 3
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
Number of leading zeros returned : 0