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


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


Java.lang.Integer.numberOfTrailingZeros()是一种方法,该方法返回最低位(即,最右或最低有效“1”位)之后的零(0)位总数(one-bit,以指定整数值或二进制数的二进制补码表示)我们可以说这是将int值转换为Binary然后考虑最低位并返回no的函数。后面的零位。如果指定的整数值在其补码表示形式中不包含one-bits,也就是说,如果该值等于零,则将返回32。

用法:

public static int numberOfTrailingZeros(int a)

参数:参数a是整数值。


返回值:此方法返回指定int值的二进制补码二进制表示形式中最低顺序的one-bit或set-bit后面的零位数;如果该值等于零,则返回32。

说明

  • 考虑一个整数a = 170
  • 二进制表示= 10101010
  • 最低一位= 2
  • 尾随零的个数= 1

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

程序1:为正数。

// Java program to illustrate the 
// Java.lang.Integer.numberOfTrailingZeros() method 
import java.lang.*; 
  
public class TrailingZeros { 
  
public static void main(String[] args) { 
  
    int a = 155; 
    System.out.println("Integral Number = " + a); 
  
  
    // Returns the number of zero bits following the lowest-order  
        //rightmost one-bit  
    System.out.print("Number of Trailing Zeros = "); 
    System.out.println(Integer.numberOfTrailingZeros(a)); 
         a = 24; 
    System.out.println("Integral Number = " + a); 
  
  
    // Returns the number of zero bits following the lowest-order  
    //rightmost one-bit  
    System.out.print("Number of Trailing Zeros = "); 
    System.out.println(Integer.numberOfTrailingZeros(a)); 
      
} 
}
输出:
Integral Number = 155
Number of Trailing Zeros = 0
Integral Number = 24
Number of Trailing Zeros = 3

注意:在这里,尾随零的数目等于155的0,这是因为在二进制表示10011011中,没有零跟随最低位(即,最右或最低有效“1”位)one-bit。

程序2:为负数。

// Java program to illustrate the 
// Java.lang.Integer.numberOfTrailingZeros() method 
import java.lang.*; 
  
public class TrailingZeros { 
  
public static void main(String[] args) { 
  
    int a = -1; 
    System.out.println("Integral Number = " + a); 
  
  
    // Returns the number of zero bits following the lowest-order  
    //rightmost one-bit  
    System.out.print("Number of Trailing Zeros = "); 
    System.out.println(Integer.numberOfTrailingZeros(a)); 
        a = -90; 
    System.out.println("Integral Number = " + a); 
  
  
    // Returns the number of zero bits following the lowest-order  
    //rightmost one-bit  
    System.out.print("Number of Trailing Zeros = "); 
    System.out.println(Integer.numberOfTrailingZeros(a)); 
      
} 
}
输出:
Integral Number = -1
Number of Trailing Zeros = 0
Integral Number = -90
Number of Trailing Zeros = 1

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

// Java program to illustrate the 
// Java.lang.Integer.numberOfTrailingZeros() method 
import java.lang.*; 
  
public class TrailingZeros { 
  
public static void main(String[] args) { 
  
     System.out.println("Number of trailing zeros = "+ 
     Integer.numberOfTrailingZeros(12.66)); 
      
} 
}

输出:

prog.java:10:error:incompatible types:possible lossy conversion from double to int
     Integer.numberOfTrailingZeros(12.66));
                                   ^
Note:Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error

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

// Java program to illustrate the 
// Java.lang.Integer.numberOfTrailingZeros() method 
import java.lang.*; 
  
public class TrailingZeros { 
  
public static void main(String[] args) { 
  
 System.out.println("Number of trailing zeros = "
                           + Integer.numberOfTrailingZeros("12")); 
} 
}

输出:

prog.java:10:error:incompatible types:String cannot be converted to int
                           + Integer.numberOfTrailingZeros("12"));
                                                           ^
Note:Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error


相关用法


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