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


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



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

用法:

public static long lowestOneBit(long num)
參數:
num - the number passed 
返回:
long value by only considering lowest 1 bit in the argument.

例子:


Input : 36 
Output : 4 
Explanation: Binary Representation = 0010 0100
It considers lowest bit(at 3) and now reset rest of the bits i.e. 0000 0100
so result = 0100 i.e. 4 or in simple terms, the first set 
bit position from right is at position 3, hence 2^2=4  


Input : 1032
Output : 8 

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

程序1:

// Java program that demonstrates the use of 
// Long.lowestOneBit() function 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        long l = 1032; 
  
        // returns a long 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 = " + Long.lowestOneBit(l)); 
  
        l = 45; 
        System.out.println("Lowest one bit = " + Long.lowestOneBit(l)); 
    } 
}

輸出:

Lowest one bit = 8
Lowest one bit = 1

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

// java program that demonstrates the use of 
// Long.lowestOneBit() function 
// negative number 
  
// include lang package 
import java.lang.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        long l = -12; 
  
        // 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 lowest-order ("rightmost") one-bit in the specified int value. 
        System.out.println("Lowest one bit = " + Long.lowestOneBit(l)); 
    } 
}

輸出:

Binary = 1111111111111111111111111111111111111111111111111111111111110100
Lowest one bit = 4

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

程序3:在參數中傳遞十進製值時。

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

輸出:

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

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

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

輸出:

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


相關用法


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