LAST UPDATED: AUGUST 27, 2020
Java Integer lowestOneBit() Method
Java lowestOneBit()
method is a part of the Integer
class of the java.lang
package. This method is used to return the single one-bit value of integer passed as argument in the position of the lowest order(rightmost) and returns zero if the passed argument is zero.
For example, the input of int 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 int lowestOneBit(int n)
Parameters:
The parameter passed is the integer value whose lowest order bit is to be returned.
Returns:
Returns the single one-bit integer 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)
{
int i=17;
int j=-12;
System.out.println(" lowest-order one-bit Integer is: "+Integer.lowestOneBit(i));
System.out.println(" lowest-order one-bit Integer is: "+Integer.lowestOneBit(j));
}
}
lowest-order one-bit Integer is: 1
lowest-order one-bit Integer 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);
Integer i = sc.nextInt();
System.out.println("Lowest-order one-bit Integer is: "+Integer.lowestOneBit(i));
}
catch(Exception e)
{
}
}
}
Enter the number : 87
Lowest-order one-bit Integer is: 1
**************************************
Enter the number : -65
Lowest-order one-bit Integer 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.