JAVA朗,朗。highestOneBit() 是Java中的一个内置方法,它首先将数字转换为二进制,然后从左侧查找第一个设置位,然后重置其余位,然后返回值。在简单语言中,如果一个数字的二进制表达式包含单个集合位的最小值,它将返回2^(右1的最后一个集合位位置)。如果二进制表达式不包含任何设置位,则返回0。
null
语法:
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。朗,朗。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"));
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END