Java Long - highestOneBit() Method
The java.lang.Long.highestOneBit() method returns a long value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified long value. Returns zero if the specified value has no one-bits in its two's complement binary representation, that is, if it is equal to zero.
Syntax
public static long highestOneBit(long i)
Parameters
i |
Specify the value whose highest one bit is to be computed. |
Return Value
Returns a long value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.
Exception
NA.
Example:
In the example below, the java.lang.Long.highestOneBit() method returns a long value with a single one-bit, in the position of the highest-order one-bit in the specified value.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating long value long x = 135; //printing x System.out.println("The x is = " + x); //printing the binary representation of x System.out.println("The x in binary is = " + Long.toBinaryString(x)); //printing the highest-order one-bit System.out.println("Highest-order one-bit = " + Long.highestOneBit(x)); } }
The output of the above code will be:
The x is = 135 The x in binary is = 10000111 Highest-order one-bit = 128
❮ Java.lang - Long