Java.lang Package Classes

Java Long - lowestOneBit() Method



The java.lang.Long.lowestOneBit() method returns a long value with at most a single one-bit, in the position of the lowest-order ("rightmost") 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 lowestOneBit(long i)

Parameters

i Specify the value whose lowest one bit is to be computed.

Return Value

Returns a long value with a single one-bit, in the position of the lowest-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.lowestOneBit() method returns a long value with a single one-bit, in the position of the lowest-order one-bit in the specified value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating long value
    long x = 120;

    //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 lowest-order one-bit
    System.out.println("Lowest-order one-bit = " + Long.lowestOneBit(x)); 
  }
}

The output of the above code will be:

The x is = 120
The x in binary is = 1111000
Lowest-order one-bit = 8

❮ Java.lang - Long