Find Last Set Bit Calculator
Enter a binary value and locate its highest set bit. Get the zero-based position from the LSB side, index from the left, leading zero count, bit mask, and effective binary bit length.
What Is the Last Set Bit in Binary?
A set bit is a binary digit whose value is 1. The last set bit usually refers to the most significant set bit, or the highest-positioned 1 in a nonzero binary value.
Consider the bit pattern 00101100. The leftmost 1 is located
at visible index 2, but binary bit positions are normally counted from the
right beginning at zero. That 1 is therefore at bit position 5.
This calculator reports the main answer as the zero-based bit position counted from the least significant side. It also shows the index from the left to remove any ambiguity.
How to Find the Last Set Bit
Find the Last Set Bit in 00101100
The first 1 from the left in 00101100 occurs at left index 2.
Because the pattern contains eight bits, its zero-based position from the
right is 5.
Last Set Bit and the Most Significant Set Bit
For a nonzero binary integer, the last set bit is its most significant set bit. This is the 1 bit with the greatest positional weight.
In 00101100, the set bits are at positions 5, 3, and 2.
Position 5 has the greatest weight, so it is the most significant set bit
and the last set bit.
How Are Set-Bit Positions Numbered?
Binary bit positions are typically numbered from right to left. The rightmost digit is bit 0, the next is bit 1, and so on. In an 8-bit word, the leftmost position is therefore bit 7.
Last Set Bit and Count Leading Zeros
The highest set-bit position is directly related to the number of leading zeros when the total bit width is known.
The 8-bit pattern 00101100 contains two leading zeros. The
highest set-bit position is therefore 8 − 2 − 1 = 5.
Last Set Bit and Significant Bit Length
For a positive nonzero integer represented without considering redundant leading zeros, the highest set-bit position tells you the number of significant binary digits.
If the highest set bit is position 5, six significant bit positions exist from bit 5 down through bit 0.
In 00101100, the full stored width is 8 bits, while the
minimal unsigned representation 101100 requires 6 bits.
What Is a Last Set-Bit Mask?
A last set-bit mask keeps only the highest set bit and replaces every other position with zero.
For 00101100, the highest set bit is at position 5.
Therefore the isolated mask is 00100000.
Such a mask can be useful when normalizing values, examining binary magnitude, building bitwise conditions, or verifying low-level algorithms.
Find Last Set Bit Examples
| Binary | Width | Last Set Bit | Left Index | Mask |
|---|---|---|---|---|
| 00000001 | 8 | Bit 0 | 7 | 00000001 |
| 00000100 | 8 | Bit 2 | 5 | 00000100 |
| 00001101 | 8 | Bit 3 | 4 | 00001000 |
| 00101100 | 8 | Bit 5 | 2 | 00100000 |
| 01010101 | 8 | Bit 6 | 1 | 01000000 |
| 10000000 | 8 | Bit 7 | 0 | 10000000 |
Find Last Set Bit vs Find First Set Bit
Find First Set Bit and Find Last Set Bit locate opposite ends of the active portion of a binary value.
FFS locates the least significant 1, while FLS locates the most significant 1. A number with several set bits can therefore produce very different results.
| Binary | First Set Bit | Last Set Bit | Meaning |
|---|---|---|---|
| 00101100 | Bit 2 | Bit 5 | Lowest vs highest 1 |
| 10000000 | Bit 7 | Bit 7 | Only one set bit |
| 00000001 | Bit 0 | Bit 0 | Only one set bit |
| 11111111 | Bit 0 | Bit 7 | All bits set |
What Is the Last Set Bit of Zero?
An all-zero value contains no set bit. There is therefore no valid highest set-bit position.
This calculator reports None for the set-bit position, left index, one-based position, and isolated mask when no 1 occurs in the entered bit pattern.
Do Leading Zeros Change the Last Set-Bit Position?
Adding leading zeros to a nonzero binary number does not change the zero-based position of its highest set bit when positions are measured from the right.
For example, 101100, 00101100, and
0000101100 all have their highest set bit at position 5.
Leading zeros do change the total entered width and the visible index of the first 1 from the left, which is why this calculator reports both measurements separately.
Where Finding the Last Set Bit Is Used
The highest active bit gives a quick indication of the size of a positive binary integer.
For a positive integer, the highest set-bit position determines the number of significant binary digits.
Locating the highest 1 can help determine shift amounts needed to normalize a binary value.
Isolate the highest active flag or highest-priority set position in a bit field.
Binary search techniques, integer algorithms, and low-level routines may need the position of the highest set bit.
Verify bit-width assumptions, leading zeros, masks, and high-order binary behavior.
Find Last Set Operations in Programming
Programming languages, compilers, and processor instruction sets often provide operations that identify the most significant set bit or count leading zeros.
Naming and indexing conventions vary. Some APIs return a zero-based bit index, while others return a one-based position or a leading-zero count. For this reason, comparing raw values between different functions can cause off-by-one mistakes.
This calculator displays the zero-based bit position, one-based position, and leading-zero count at the same time so those conventions can be compared easily.
Common Find Last Set Bit Mistakes
Using the visible left index as the bit position
The index inside the written binary string and the numerical bit position are different measurements. Bit positions begin from the right.
Confusing FLS with FFS
FFS finds the lowest set bit. FLS finds the highest set bit.
Counting leading zeros as part of the numerical bit length
Leading zeros belong to the entered width but do not increase the significant unsigned bit length.
Assigning a set-bit position to zero
An all-zero value has no set bits, so there is no valid FLS position.