In Java, Integer.lowestOneBit(i) returns an int with a single 1 bit, in the same position as the rightmost1 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
Call
What it does
Integer.lowestOneBit(i)
The rightmost set bit of i, as a number.
i & -i
The 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
Parameter
Description
i (int)
The number to examine.
Return Value
Return Value
Description
int
A value with a single bit set at the position of the lowest set bit of i, or 0 if i is 0.
Trailing zeros are skipped. 12 is 1100, and the rightmost 1 is worth 4.
40 and 96. 40 is 101000, so the answer is 8, and 96 is 1100000, so the answer is 32.
An odd number. 7 ends in a 1, so the answer is 1.
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);
}
}
Output
74 = 1001010
bit worth 2
bit worth 8
bit worth 64
sum of bits: 74
How It Works
74 in binary. It is 1001010, so three bits are set.
Lowest first. The first pass picks 2, the second picks 8, and the third picks 64.
Subtract to move on. Removing the bit from remaining makes the next lowest set bit the new rightmost one.
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);
}
}
}
Output
64: divisible by 64, 8-byte aligned: true
100: divisible by 4, 8-byte aligned: false
128: divisible by 128, 8-byte aligned: true
12: divisible by 4, 8-byte aligned: false
4096: divisible by 4096, 8-byte aligned: true
How It Works
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.
64, 128, and 4096. Each is a power of two at least 8, so each is aligned to 8.
100 and 12. They end in 100, so the biggest power of two dividing them is 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.