Find First Set Bit Calculator
Enter a binary value and find its first set bit, scanning from the least significant side. Get the zero-based bit position, index from the left, bit mask, width, and related binary details.
What Is the First Set Bit in Binary?
A set bit is a binary digit whose value is 1. In common low-level bit operations, finding the first set bit means locating the first 1 encountered when scanning from the least significant bit toward the most significant bit.
For example, the binary value 10110000 ends with four zeros.
The first 1 encountered from the right is therefore at bit position 4 when
positions are numbered from zero.
This calculator uses that LSB-first convention and also reports the same bit’s location from the left side so the result is easy to interpret in the visible binary string.
How to Find the First Set Bit
Find the First Set Bit in 10110000
Starting from the right side of 10110000, the first four
positions contain 0. The next position contains 1.
How Are Binary Bit Positions Numbered?
Bit positions are usually numbered from right to left. The least significant bit is position 0, the next bit is position 1, and each step to the left increases the position by one.
In an 8-bit value, the leftmost bit is therefore position 7. In a 16-bit value, the leftmost position is 15.
First Set Bit From the Right vs First 1 From the Left
The phrase “first set bit” can be ambiguous unless the search direction is specified. In many programming and bit-manipulation contexts, it means the least significant set bit: the first 1 found from the right.
By contrast, searching from the left locates the most significant set bit. These may be different positions in the same binary value.
| Binary | First Set Bit From Right | First 1 From Left | Width |
|---|---|---|---|
| 10110000 | Bit 4 | Index 0 | 8 |
| 00101100 | Bit 2 | Index 2 | 8 |
| 00000100 | Bit 2 | Index 5 | 8 |
First Set Bit and Count Trailing Zeros
For every nonzero binary integer, the position of the least significant set bit is equal to the number of trailing zeros.
For example, 1101000 ends with three zeros. Its first set bit
from the right is therefore at position 3.
What Is the First Set-Bit Mask?
A first set-bit mask contains a 1 only at the position of the least significant set bit and zeros everywhere else.
For 10110000, the first set bit is at position 4. The
corresponding mask is therefore 00010000.
This isolated-bit mask is useful in low-level algorithms because it identifies exactly one active bit without changing its original position.
Find First Set Bit Examples
| Binary | Width | First Set Bit | 1-Based Position | Mask |
|---|---|---|---|---|
| 00000001 | 8 | Bit 0 | 1 | 00000001 |
| 00000010 | 8 | Bit 1 | 2 | 00000010 |
| 00000100 | 8 | Bit 2 | 3 | 00000100 |
| 00001000 | 8 | Bit 3 | 4 | 00001000 |
| 10110000 | 8 | Bit 4 | 5 | 00010000 |
| 10000000 | 8 | Bit 7 | 8 | 10000000 |
What Happens When the Binary Value Is Zero?
An all-zero bit pattern contains no set bits. Therefore there is no valid first set-bit position.
Instead of inventing a position, this calculator reports None for the position, left index, and mask when the input contains no 1 bits.
Do Leading Zeros Change the First Set-Bit Position?
When the position is counted from the right, adding leading zeros to the left does not change the first set-bit position of a nonzero value.
For example, 100, 00100, and
00000100 all have their least significant set bit at
position 2.
Leading zeros do change the total width and the set bit’s index when measured from the left side, which is why both values are shown separately.
Where Finding the First Set Bit Is Used
Locate the lowest active flag in a packed binary mask or status word.
Extract the least significant active square or state in compact board representations.
Identify the lowest set-bit position for fast number-theory and binary operations.
Find the first enabled permission or feature flag in a bit field.
Priority encoders and low-level logic often need to identify one active bit from a binary word.
Verify bit positions, masks, alignment, and results produced by bitwise code.
Find First Set Operations in Programming
Many programming environments and processors provide operations for locating set bits efficiently. Names vary across languages and APIs, and some return a zero-based index while others return a one-based position.
This difference matters. A lowest set bit at zero-based bit position 4 corresponds to one-based position 5. The calculator reports both to make comparisons with different APIs easier.
Always check the indexing convention used by a specific programming language or processor instruction before comparing results directly.
Common Find First Set Bit Mistakes
Searching from the wrong side
Under the convention used here, the search starts at the least significant or rightmost bit.
Mixing zero-based and one-based positions
Bit indices usually start at 0, but some find-first-set APIs report positions starting at 1. These answers differ by one.
Treating zero as having a set bit
Zero contains no 1 bits, so it has no valid first set-bit location.
Confusing first set bit with most significant bit
The least significant set bit is found from the right. The most significant set bit is found from the left.