Binary Bit Analysis

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.

Highest 1 bit MSB position Isolated mask
Locate the Highest Set Bit
The last set bit is the leftmost 1, reported as its zero-based bit position from the right.
Bit Position 5
Binary value 00101100
The highest set bit is at zero-based position 5.
Bit Width 8 bits
Index From Left Index 2
1-Based Position 6
Leading Zeros 2
Last set-bit mask
00100000
Last Set Bit

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.

For a nonzero fixed-width value last set-bit position = width − leadingZeros − 1
How to Use

How to Find the Last Set Bit

1
Enter the complete binary value Type the bit pattern using only 0 and 1. Leading zeros may be included.
2
Look for the first 1 from the left The highest set bit is the first 1 encountered from the most significant side.
3
Convert that location to a bit position Bit positions are counted from the right, where the least significant bit is position 0.
4
Review the extra details The calculator also returns leading zeros, visible left index, one-based position, width, and isolated mask.
Worked Example

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 example
Binary: 00101100 Positions: 7 6 5 4 3 2 1 0 0 0 1 0 1 1 0 0 ↑ Highest / last set bit = position 5 Mask: 00100000
Most Significant Bit

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.

The leftmost digit of a fixed-width pattern is not necessarily set. Leading zeros must be skipped until the first 1 is reached.
Position Numbering

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.

8-bit numbering
Bit positions: 7 6 5 4 3 2 1 0 Binary: 0 0 1 0 1 1 0 0 Highest set bit: position 5
Leading Zeros

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.

Nonzero value FLS position = width − CLZ − 1
Bit Length

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.

Positive nonzero integer significant bit length = lastSetBitPosition + 1

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.

Set-Bit Mask

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.

Conceptual form mask = 1 << lastSetBitPosition

Such a mask can be useful when normalizing values, examining binary magnitude, building bitwise conditions, or verifying low-level algorithms.

Reference

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
FLS vs FFS

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
Zero Input

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.

Zero case
Input: 00000000 Set bits = none Last set-bit position = None Index from left = None Mask = None
Leading Zeros

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.

Applications

Where Finding the Last Set Bit Is Used

Binary Magnitude

The highest active bit gives a quick indication of the size of a positive binary integer.

Bit Length

For a positive integer, the highest set-bit position determines the number of significant binary digits.

Normalization

Locating the highest 1 can help determine shift amounts needed to normalize a binary value.

Bit Masks

Isolate the highest active flag or highest-priority set position in a bit field.

Algorithms

Binary search techniques, integer algorithms, and low-level routines may need the position of the highest set bit.

Debugging

Verify bit-width assumptions, leading zeros, masks, and high-order binary behavior.

Programming

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 Mistakes

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.

FAQ

Find Last Set Bit Calculator FAQs

The last set bit is the most significant or highest-positioned 1 bit in a nonzero binary value.
The highest 1 occurs at zero-based bit position 5.
For a nonzero value, it is the most significant set bit. The physical MSB position of a fixed-width word may still be zero when leading zeros are present.
Positions are counted from right to left beginning with bit 0 at the least significant end.
The only set bit is at zero-based bit position 7, so the last set bit is 7.
Zero contains no set bits, so this calculator returns None rather than inventing a set-bit position.
For a nonzero value, FLS position equals width minus leading-zero count minus one.
For a positive nonzero integer, significant bit length equals the last set-bit position plus one.
FLS locates the highest set bit. FFS locates the lowest set bit.
No, not when the bit position is counted from the right. They do change the full width and left-side index.
It is a binary mask with a 1 only at the highest set-bit position and zeros everywhere else.
Yes. The logic scans the binary characters directly instead of converting the complete input into an ordinary fixed-precision JavaScript number.
Scroll to Top