Position of the leftmost set bit in a given binary string where all 1s appear at the end

The aim of this article is to implement a program to find the position of the leftmost set bit in a given binary string where all 1s appear at the end.

A binary string is a sequence of bits (0s and 1s) used to represent data in binary format. In this problem, we are given a binary string where all the 1s are grouped together at the rightmost positions, and we need to find the leftmost position where the first '1' appears.

Syntax

int findFirstBit(char* str, int length);

Problem Statement

Given a binary string where all 1s appear at the end, find the position of the leftmost set bit (first '1' from left). If no set bit is found, return -1.

Example 1

Input: str = "0000111", length = 7

Output: 4

Explanation Index 4 corresponds to the first set bit from the left side.

Example 2

Input: str = "0111", length = 4

Output: 1

Explanation Index 1 corresponds to the first set bit from the left side.

Approach: Binary Search

Since all 1s appear at the end, we can use binary search to efficiently find the leftmost set bit. The idea is to search for the transition point where 0 changes to 1.

Algorithm

  • Step 1 Initialize left = 0, right = length-1, and result = -1
  • Step 2 Apply binary search to find the first occurrence of '1'
  • Step 3 If middle element is '1', search in the left half
  • Step 4 If middle element is '0', search in the right half
  • Step 5 Return the position of the first set bit

Example

Here is the C program implementation to find the position of the leftmost set bit ?

#include <stdio.h>
#include <string.h>

int isBitSet(char *str, int i) {
    return str[i] == '1';
}

int findFirstBit(char* str, int l) {
    int left = 0, right = l - 1, mid, res = -1;
    
    while (left <= right) {
        mid = (left + right) / 2;
        
        if (isBitSet(str, mid)) {
            res = mid;
            right = mid - 1;  // Search in left half
        } else {
            left = mid + 1;   // Search in right half
        }
    }
    
    return res;
}

int main() {
    char str1[] = "0000111";
    char str2[] = "0111";
    char str3[] = "0000";
    
    printf("Binary string: %s, Leftmost set bit at position: %d<br>", 
           str1, findFirstBit(str1, strlen(str1)));
    
    printf("Binary string: %s, Leftmost set bit at position: %d<br>", 
           str2, findFirstBit(str2, strlen(str2)));
    
    printf("Binary string: %s, Leftmost set bit at position: %d<br>", 
           str3, findFirstBit(str3, strlen(str3)));
    
    return 0;
}
Binary string: 0000111, Leftmost set bit at position: 4
Binary string: 0111, Leftmost set bit at position: 1
Binary string: 0000, Leftmost set bit at position: -1

Time Complexity

The time complexity of this approach is O(log n) where n is the length of the binary string, due to the binary search algorithm. The space complexity is O(1) as we use only constant extra space.

Conclusion

Using binary search, we can efficiently find the leftmost set bit in a binary string where all 1s appear at the end. This approach takes advantage of the sorted nature of the problem to achieve logarithmic time complexity.

Updated on: 2026-03-15T14:33:54+05:30

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements