当前位置: 首页>>代码示例>>用法及示例精选>>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。