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"));
相关用法
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Set contains()用法及代码示例
- Java Set add()用法及代码示例
- Java Map get()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Java lang.Long.highestOneBit() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。