Java Integer lowestOneBit() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.lowestOneBit(i) returns an int with a single 1 bit, in the same position as the rightmost 1 bit of i. Every other bit is zero.

For 12, which is 1100, the result is 100, which is 4. It is the same as i & -i, written so that you can read it.

A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.lowestOneBit(...). Integer lives in java.lang, so no import is needed.

Syntax

One argument: the int to examine.

Java
int a = Integer.lowestOneBit(12);    // 4   (1100 -> 0100)
int b = Integer.lowestOneBit(40);    // 8
int c = Integer.lowestOneBit(0);     // 0
CallWhat it does
Integer.lowestOneBit(i)The rightmost set bit of i, as a number.
i & -iThe same thing written with operators.
n -= Integer.lowestOneBit(n)Removes the rightmost set bit from n.
Integer.highestOneBit(i)The leftmost set bit instead.

The result is the largest power of two that divides i evenly, for any non-zero i.

Library declarations
Java
// java.lang.Integer
public static int lowestOneBit(int i)

Parameters

ParameterDescription
i (int)The number to examine.

Return Value

Return ValueDescription
intA value with a single bit set at the position of the lowest set bit of i, or 0 if i is 0.

Compare with Similar Methods

Related bit methods:

MethodHow it differs
highestOneBit(i)Isolates the leftmost set bit.
numberOfTrailingZeros(i)Gives the position of the lowest set bit rather than its value.
bitCount(i)Counts how many set bits there are.
toBinaryString(i)Shows the bits to compare.

Java Examples

Example 1 — Isolate the Lowest Set Bit

Print the lowest set bit for several numbers, with their binary forms.

Java
public class IntegerLowestOneBitDemo {
    public static void main(String[] args) {
        int[] numbers = {12, 40, 7, 96, 0, -8};

        for (int n : numbers) {
            System.out.println(n + " -> " + Integer.lowestOneBit(n)
                    + "  (" + Integer.toBinaryString(n & 0xFF) + ")");
        }
    }
}

How It Works

  1. Trailing zeros are skipped. 12 is 1100, and the rightmost 1 is worth 4.
  2. 40 and 96. 40 is 101000, so the answer is 8, and 96 is 1100000, so the answer is 32.
  3. An odd number. 7 ends in a 1, so the answer is 1.
  4. Zero and negatives. 0 has no bits, so the result is 0, and -8 (the low byte is 11111000) gives 8.

Example 2 — List Every Set Bit of a Number

Repeat lowestOneBit and remove the bit each time, to list the powers of two that add up to a number.

Java
public class IntegerLowestOneBitPeel {
    public static void main(String[] args) {
        int number = 74;
        int remaining = number;
        int total = 0;

        System.out.println(number + " = " + Integer.toBinaryString(number));
        while (remaining != 0) {
            int bit = Integer.lowestOneBit(remaining);
            System.out.println("  bit worth " + bit);
            total += bit;
            remaining -= bit;
        }
        System.out.println("sum of bits: " + total);
    }
}

How It Works

  1. 74 in binary. It is 1001010, so three bits are set.
  2. Lowest first. The first pass picks 2, the second picks 8, and the third picks 64.
  3. Subtract to move on. Removing the bit from remaining makes the next lowest set bit the new rightmost one.
  4. The loop ends at zero. Adding 2 + 8 + 64 gives back 74.

Example 3 — Check Whether Addresses Are Aligned

Use the lowest set bit to test whether a number is a multiple of 8, and find its largest power-of-two divisor.

Java
public class IntegerLowestOneBitAlign {
    public static void main(String[] args) {
        int[] addresses = {64, 100, 128, 12, 4096};

        for (int address : addresses) {
            int size = Integer.lowestOneBit(address);
            boolean aligned8 = size >= 8;
            System.out.println(address + ": divisible by " + size + ", 8-byte aligned: " + aligned8);
        }
    }
}

How It Works

  1. The lowest bit is the biggest power of two divisor. A number ending in 1000 in binary is a multiple of 8, but not of 16.
  2. 64, 128, and 4096. Each is a power of two at least 8, so each is aligned to 8.
  3. 100 and 12. They end in 100, so the biggest power of two dividing them is 4.
  4. A simple test. Comparing the result with 8 answers the alignment question without any division.

Frequently Asked Questions

It keeps only the rightmost 1 bit of a number. For 12 (1100) the result is 4 (0100).
Yes. That is exactly how it is calculated; the method name just makes the intention clear.
Repeat bit = Integer.lowestOneBit(n) and then n -= bit until n becomes 0.
It returns 0, because there is no set bit.
For a non-zero number, lowestOneBit(n) is the largest power of two that divides n evenly.

Next: max()

Return the larger of two int values.

max() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful