當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java lang.Long.highestOneBit()用法及代碼示例



java.lang.Long.highestOneBit()是Java中的內置方法,該方法首先將數字轉換為Binary,然後從左側開始尋找第一個置位的位,然後重置其餘位,然後返回值。用簡單的語言,如果一個數字的二進製表達式包含一個設置位的最小值,則它返回2 ^(從right-1開始的最後一個設置位的位置)。如果二進製表達式不包含任何設置位,則返回0。

用法:

public static long highestOneBit(long num)
Parameters:
num - the number passed 
Returns:
long value by only considering highest 1 bit in the argument.

例子:


Input:9 
Output:8
Explanation:Binary Representation = 1001
It considers highest bit(at 4th from right) and now
reset rest of the bits i.e. 1000
so result = 1000 i.e. 8 or in simple terms, the last set 
bit position from right is at position 4, hence 2^3=8

Input:45
Output:32

以下示例程序旨在說明java.lang.Long.highestOneBit()函數:

程序1:

// Java program that demonstrates the use of 
// Long.highestOneBit() function 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        long l = 9; 
  
        // returns a long value with at most a 
        // single one-bit, in the position 
        // of the highest-order ("rightmost") 
        // one-bit in the specified long value. 
        System.out.println("highest one bit = " + Long.highestOneBit(l)); 
  
        l = 45; 
        System.out.println("highest one bit = " + Long.highestOneBit(l)); 
    } 
}

輸出:

highest one bit = 8
highest one bit = 32

注意:負數的最高位在每種情況下都將始終相同,因此,不管數字是多少,每個負數的輸出都將相同。

程序2:下麵的程序演示了傳遞負數時函數的使用。

// java program that demonstrates the use of 
// Long.highestOneBit() function 
// negative number 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        long l = -15; 
  
        // prints the binary of a negative expression 
        System.out.println("Binary = " + Long.toBinaryString(l)); 
  
        // returns a long value with at 
        // most a single one-bit, in the position 
        // of the highest-order ("rightmost") 
        // one-bit in the specified int value. 
        System.out.println("Highest one bit = " + Long.highestOneBit(l)); 
    } 
}

輸出:

Binary = 1111111111111111111111111111111111111111111111111111111111110001
Highest one bit = -9223372036854775808

當將十進製字符串值作為參數傳遞時,它將返回錯誤消息。
程序3:在參數中傳遞十進製值時。

// java program that demonstrates the 
// Long.highestOneBit() function 
// decimal value in argument 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        System.out.println("highest one bit = " + Long.highestOneBit(12.34)); 
    } 
}

輸出:

prog.java:13:error:incompatible types:possible lossy conversion from double to long
        System.out.println("highest one bit = " + Long.highestOneBit(12.34));

程序3:在參數中傳遞字符串值時。

// java program that demonstrates the 
// Long.highestOneBit() function 
// string value in argument 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        System.out.println("highest one bit = " + Long.highestOneBit("12")); 
    } 
}

輸出:

prog.java:13:error:incompatible types:String cannot be converted to long
        System.out.println("highest one bit = " + Long.highestOneBit("12"));


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Java lang.Long.highestOneBit() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。