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
相關用法
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.ArrayList.addall()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Java.math.BigInteger.probablePrime()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Integer.numberOfTrailingZeros() Method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。