📜  Java Java () 方法与示例

📅  最后修改于: 2022-05-13 01:54:30.663000             🧑  作者: Mango

Java Java () 方法与示例


Java.lang.Long.lowestOneBit()是Java中的一个内置方法,它首先将数字转换为二进制,然后查找最低位置的第一个设置位,然后重置其余位,然后返回值。用简单的语言来说,如果一个数字的二进制表达式至少包含一个设置位,则它返回 2^(从右 1 开始的第一个设置位位置)。如果二进制表达式不包含任何设置位,则返回 0。

句法:

public static long lowestOneBit(long num)
Parameters:
num - the number passed 
Returns:
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()函数:

方案一:

// 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"));