java.lang.Integer.numberOfTrailingZeros()方法實例
java.lang.Integer.numberOfTrailingZeros() 返回零位以下的最低階(“最右”)的數在指定的int值的二進製補碼表示一比特。
它返回32,如果指定的值冇有一個比特在它的2的補碼表示,換句話說,如果它等於零。
聲明
以下是java.lang.Integer.numberOfTrailingZeros()方法的聲明
public static int numberOfTrailingZeros(int i)
參數
-
i -- 這是int值。
返回值
此方法返回零位的最高位(“最左側”)前在指定的int值的二進製補碼表示法,或32個1位的數量,如果該值為零。
異常
-
NA
例子
下麵的例子顯示java.lang.Integer.numberOfTrailingZeros()方法的使用。
package com.yiibai; import java.lang.*; public class IntegerDemo { public static void main(String[] args) { int i = 170; System.out.println("Number = " + i); /* returns the string representation of the unsigned integer value represented by the argument in binary (base 2) */ System.out.println("Binary = " + Integer.toBinaryString(i)); // returns the number of one-bits System.out.println("Number of one bits = " + Integer.bitCount(i)); /* returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value */ System.out.println("Highest one bit = " + Integer.highestOneBit(i)); /* returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.*/ System.out.println("Lowest one bit = " + Integer.lowestOneBit(i)); /*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(i)); /* 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(i)); } }
讓我們編譯並運行上述程序,這將產生以下結果:
Number = 170 Binary = 10101010 Number of one bits = 4 Highest one bit = 128 Lowest one bit = 2 Number of leading zeros = 24 Number of trailing zeros = 1