Integer lowestOneBit() Method in Java

The Integer.lowestOneBit() method of java.lang is an inbuilt function that returns an int value with at most a single one-bit, in the position of the lowest-order (ie.rightmost) one-bit in the specified int value. This method will return zero if the specified value has no one-bits in its two’s complement binary representation, ie. if the binary representation of the number is equal to zero.

Syntax:

public static int lowestOneBit(int a)

Parameters: The method takes one parameter a of integer type that refers to the value whose lowest order bit is to be returned or on which the operation is to be performed.

Returns : The method can return two types of values:

  • Returns an integer value with a single 1-bit, in the position of the lowest-order one-bit in the specified value
  • Returns zero if the specified value is equal to zero.

Examples:

Input: 157
Output: Lowest one bit = 1

Input: 0
Output: Lowest one bit = 0

Explanation:
Consider any integer a = 10
Binary Representation = 0000 1010
Lowest bit(at 1) i.e.0000 0010
so result = 2^1=2

Below programs illustrate the java.lang.Integer.lowestOneBit() method:

Program 1: For a positive number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the
// java.lang.Integer.lowestOneBit() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = 157;
        System.out.println("Given Number = " + a);
  
        // Returns an int value with at most a single one-bit
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
  
        a = 64;
        System.out.println("Given Number = " + a);
  
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
  
        // Here it will return 0 since the number is itself zero
        a = 0;
        System.out.println("Given Number = " + a);
  
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
    }
}

chevron_right


Output:

Given Number = 157
Lowest one bit = 1
Given Number = 64
Lowest one bit = 64
Given Number = 0
Lowest one bit = 0

Program 2: For a negative number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the
// java.lang.Integer.lowestOneBit() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        int a = -157;
        System.out.println("Given Number = " + a);
  
        // It will return an int value with at most a single one-bit
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
        a = -17;
        System.out.println("Given Number = " + a);
  
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
    }
}

chevron_right


Output:

Given Number = -157
Lowest one bit = 1
Given Number = -17
Lowest one bit = 1

Program 3: For a decimal value and String.
Note:It returns a compile time error message when a decimal value and a string is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java praogram to illustrate the
// java.lang.Integer.lowestOneBit() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        // Decimal value is given
        int a = 71.57;
        System.out.println("Given Number = " + a);
  
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
  
        // String is passed
        a = "12";
        System.out.println("Given Number = " + a);
  
        System.out.print("Lowest one bit = ");
        System.out.println(Integer.lowestOneBit(a));
    }
}

chevron_right


Output:

prog.java:10: error: incompatible types: possible lossy conversion from double to int
    int a = 71.57;
            ^
prog.java:17: error: incompatible types: String cannot be converted to int
    a = "12";
        ^
2 errors


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.