java.lang.Long.highestOneBit()方法實例
java.lang.Long.highestOneBit() 方法在指定long值返回一個long值至多單個1位,在最高位(「最左」)1位的位置。它返回零,如果指定的值沒有一個位在其二進制補碼錶示法,即,如果它等於零。
聲明
以下是**java.lang.Long.highestOneBit()**方法的聲明
public static long highestOneBit(long i)
參數
- i -- 這是long 值。
返回值
該方法返回一個long 值與單個1位,在指定的值最高的一階位的位置,或者零,如果指定值本身等於零。
異常
- NA
例子
下面的例子顯示java.lang.Long.highestOneBit()方法的使用。
package com.yiibai; import java.lang.*; public class LongDemo { public static void main(String[] args) { long l = 220; System.out.println("Number = " + l); /* returns the string representation of the unsigned long value
represented by the argument in binary (base 2) */ System.out.println("Binary = " + Long.toBinaryString(l)); // returns the number of one-bits System.out.println("Number of one bits = " + Long.bitCount(l)); /* returns a long 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 = " + Long.highestOneBit(l)); } }
讓我們來編譯和運行上面的程序,這將產生以下結果:
Number = 220 Binary = 11011100 Number of one bits = 5 Highest one bit = 128