How to use the Last set bit Calculator tool
- Type or paste a binary word into the Binary digits box, for example 01001101.
- Select Convert. Select Example instead to load the sample word and run it at once.
- Read the zero-based index of the highest set bit in the Result box.
- Select Copy to copy the index, or Clear to reset the form.
What bit scan reverse returns
Bit scan reverse (BSR) searches a word from its most significant end and stops at the first 1 it meets. The answer is that bit's position, where the rightmost bit is position 0 and positions grow to the left. For a nonzero value x, the result equals floor(log2(x)): the exponent of the largest power of two that is not greater than x.
The name comes from the x86 BSR instruction. Its partner, bit scan forward, finds the lowest set bit. On this site the reverse scan is listed as the Last set bit Calculator and the forward scan as the First set bit Calculator.
Worked example: 01001101
Number the bits of 01001101 from the right: bit 0 is 1, bit 1 is 0, bits 2 and 3 are 1, bits 4 and 5 are 0, bit 6 is 1 and bit 7 is 0. The highest position holding a 1 is 6, so the tool prints 6. The value is 77 in decimal, and 2^6 = 64 is the largest power of two that fits in it.
01001101 → 6
0000000001001101 → 6
00010110 → 4
0000 → No set bitInput rules and the all-zero case
The scanner reads the field as one unsigned word. It strips an optional 0b prefix and every space or line break, so grouped input such as 0100 1101 is fine.
The zero case deserves attention in real code too. On x86, BSR leaves its destination undefined when the source is zero and only sets the zero flag, so callers must test for zero first.
- Only the digits 0 and 1 are accepted. Hex or decimal input is rejected.
- The word can hold from 1 to 65,536 bits.
- Anything else produces the message Enter between 1 and 65,536 binary digits.
- A word with no 1 in it returns the text No set bit, not a number.
Bit scan reverse in code and its link to leading zeros
For a word of width w and a nonzero value, the highest set bit index is w - 1 minus the number of leading zeros. That is how most languages expose it: GCC and Clang offer __builtin_clz, MSVC offers _BitScanReverse, C++20 has std::bit_width(x) - 1, Python has x.bit_length() - 1 and Rust has ilog2.
Typical uses are computing an integer logarithm, picking a size class in a memory allocator, locating the highest-priority ready task in a scheduler bitmap and rounding a number up to the next power of two.
Frequently asked questions
What is the difference between bit scan reverse and bit scan forward?
Bit scan reverse finds the highest set bit, the 1 closest to the left end of the word. Bit scan forward finds the lowest set bit, the 1 closest to the right end. For 01001101 the reverse scan gives 6 and the forward scan gives 0.
Is the highest set bit index the same as log2?
For any nonzero unsigned value it equals the base-2 logarithm rounded down. 01001101 is 77, the index is 6, and 2^6 = 64 is the largest power of two not above 77.
What does the tool show when the input is all zeros?
It shows the text No set bit. There is no 1 to find, so no index exists. This mirrors hardware, where the x86 BSR instruction gives an undefined result for a zero operand.
Do leading zeros change the result?
No. The index counts from the rightmost bit, so 1001101, 01001101 and a 32-bit version of the same value all return 6. Leading zeros matter for a leading zero count, which depends on the word width.
Is the binary word sent to a server?
No. The scan is a few lines of JavaScript that run in your browser tab. The digits you enter are not uploaded or stored.