How to use the First set bit Calculator tool
- Paste the binary word into the Binary digits box, for example 01001101.
- Select Convert to scan from the least significant bit upward.
- Read the zero-based index of the lowest 1 in the Result box. The sample word returns 0 because its last bit is set.
- If the result reads No set bit, the input contains only zeros. Select Clear to try another word.
What bit scan forward means
Bit scan forward searches a word from the least significant bit toward the most significant bit and stops at the first 1 it meets. The answer is the index of that bit. Index 0 is the rightmost digit, index 1 is the digit to its left, and so on. The name comes from the x86 instruction BSF, and the same operation is known as find first set or count trailing zeros.
For any nonzero word the index equals the number of zeros to the right of the lowest 1. That is why 1011000 returns 3: three zeros trail the last 1.
First set bit examples
Leading zeros have no effect, since counting starts on the right: 1011000 and 01011000 both return 3. Spaces and a 0b prefix are ignored too, so you can paste 0b1010 1000 as it stands. The scan runs in your browser without contacting a server.
01001101 -> 0
10110100 -> 2
1011000 -> 3
10000000 -> 7
0000 -> No set bitThe all-zeros case and indexing conventions
A word with no 1 bits has no first set bit, and implementations disagree on what to return. This tool prints No set bit. The Trailing zeros Calculator on this site returns the word length for that input. On x86, BSF leaves its destination undefined and sets the zero flag, while the newer TZCNT instruction returns the operand size. In C, __builtin_ctz(0) is undefined behavior.
Watch for one-based variants. The POSIX function ffs() and GCC's __builtin_ffs return the index plus one, and 0 when no bit is set. Subtract one from their result to compare with this calculator.
Where finding the lowest set bit is useful
Finding the lowest 1 quickly is a building block in systems code. Typical cases include the following.
- Bitmap allocators: the first set bit in a free map is the next free block or file descriptor slot.
- Schedulers: a ready-queue bitmap is scanned to pick the next runnable priority level.
- Chess engines: bitboards are walked one piece at a time by scanning and clearing the lowest bit.
- Alignment: 1011000 (88) has index 3, so the address is a multiple of 8 but not of 16.
- Binary GCD and Fenwick trees, which strip or step by the lowest set bit using x AND -x.
Frequently asked questions
What is the first set bit of 01001101?
The index is 0. The rightmost digit of the word is 1, so the scan stops immediately. Indexes are zero-based and counted from the right.
Is bit scan forward the same as counting trailing zeros?
For any word containing a 1, yes. The index of the lowest set bit equals the number of zeros below it. They differ only for an all-zeros input, where this tool reports No set bit.
What is the difference between bit scan forward and bit scan reverse?
Forward finds the lowest set bit and reverse finds the highest. For 10110100, the forward scan returns 2 and the reverse scan returns 7. The Last set bit Calculator covers the reverse direction.
How do I get the value of the lowest set bit and not its index?
Raise 2 to the reported index. Index 3 means the bit is worth 8. In code the usual shortcut is x & -x, which isolates the lowest set bit directly.
Does the result count from 0 or from 1?
From 0. The least significant bit has index 0, matching BSF, TZCNT and most ctz functions. Add one if you need the ffs() convention used by the C library.
How long can the input be?
Between 1 and 65,536 binary digits after spaces and an optional 0b prefix are removed. Longer input or characters other than 0 and 1 produce an error.