当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Integer.numberOfLeadingZeros()用法及代码示例


java.lang.Integer.numberOfLeadingZeros()是一种返回highest-order之前的零(0)位总数的方法(即,最左或最高有效“1”位)one-bit,以指定整数值或我们可以的二进制补码表示说这是将int值转换为Binary然后认为最高的一位并返回no的函数。前面的零位。如果指定的整数值在其补码表示形式中没有one-bits,也就是说,如果它等于零,则将返回32。

用法:

public static int numberOfLeadingZeros(int arg)

参数:此方法接受单个参数arg,它是整数值。


返回值:此方法以指定的int值的二进制补码表示形式返回highest-order set-bit之前的零位;如果该值等于零,则返回32。

说明:

  • 考虑任何整数arg = 19
  • 二进制表示= 0001 0011
  • 最高位(5位),即0001 0000
  • 因此结果= 16-5,即11

以下示例程序旨在说明java.lang.Integer.numberOfLeadingZeros()方法。

程序1:为正数。

// 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

程序2:为负数。

// 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

程序3:对于十进制值。
注意:当将十进制值作为参数传递时,它将返回错误消息。

// 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)); 
    } 
}

输出:

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

程序4:对于字符串值,请在参数中传递。
注意:当将字符串作为参数传递时,它将返回错误消息。

// 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")); 
    } 
}

输出:

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


相关用法


注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 Integer.numberOfLeadingZeros() Method in Java With Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。