Signup/Sign In

Java Long lowestOneBit() Method

Java lowestOneBit() method is a part of the Long class of the java.lang package. This method is used to return the single one-bit long value of long passed as an argument in the position of the lowest order(rightmost) and returns zero if the passed argument is zero.

For example, the input of long 17 will return 1; As 17 can be represented in binary as 10001 so it will return the nearest bit right which is equal to 1.

Syntax:

public static long lowestOneBit(long i)

Parameters:

The parameter passed is the long value whose lowest order bit is to be returned.

Returns:

Returns the single one-bit long value in the position of the lowest order and zero if the parameter passed is zero.

Example 1:

Here, the binary equivalent of 17 is 10001, hence the lowest order one bit is 1 and in case of negative binary the equivalent two's complement will be considered and the lowest bit is taken accordingly.

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        long i=17;
        long j=-12;
        
        System.out.println(" lowest-order one-bit Long is: "+Long.lowestOneBit(i));  
        System.out.println(" lowest-order one-bit Long is: "+Long.lowestOneBit(j));  
    }  
}


lowest-order one-bit Long is: 1
lowest-order one-bit Long is: 4

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
           System.out.print("Enter the number : ");  
           Scanner sc = new Scanner(System.in);  
           Long i = sc.nextLong();  
           System.out.println("Lowest-order one-bit Long is: "+Long.lowestOneBit(i));  
        }
        
        catch(Exception e)
        {
          
        }
    }
} 


Enter the number : 95232
Lowest-order one-bit Long is: 1024
*****************************************
Enter the number : -4765
Lowest-order one-bit Long is: 1

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.