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 Position the leftmost set bit in a given binary string where all 1s appear at the end.

A string of bits is known as a binary string. A binary string is utilized for storing non-traditional data, such as images, as opposed to a character string, which typically holds text data. The quantity of bytes in a binary string determines its length.

Binary data, or data that is portrayed in a binary (base-2) version instead of a text (base-10) format, is stored in binary string variables in computer programming.

Provided a binary string Str having the length L where all 1s are arranged in the rightmost position. If no set bit can be detected from the left side, the responsibility is to return -1.

Example 1

Let us take the Input string:
str = 0000111
With size, l = 7
Output obtained in this case is: 4

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

Example 2

Let us take the Input string:
str = 0111
With size, l = 4
Output obtained in this case is: 1

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

Example 3

Let us take the Input string:
str = 001
With size, l = 3
Output obtained in this case is: 2

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

Example 4

Let us take the Input string:
str = 0001
With size, l = 4
Output: 3

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

Problem Statement

Implement a program to obtain the position the leftmost set bit in a given binary string where all 1s appear at the end.

Approach

The approach to solve this problem and obtain the Position of the leftmost set bit in a given binary string where all 1s appear at the end is to apply the technique of binary searching.

Let us give you a glimpse on binary searching techniques.

A sorted array can be searched using the binary search algorithm by continually halving the search interval. Utilizing the knowledge that each array is sorted, binary search attempts to minimize the time complexity down O(log N).

Algorithm

The algorithm to implement a program to obtain the Position of the leftmost set bit in a given binary string where all 1s appear at the end is given below

  • Step 1 − Define a function "isBitSet" to determine whether or not a bit is set.

  • Step 2 − Define a function "findFirstBit" to determine where the first set bit is located

  • Step 3 − Define integer variables left, right, mid and res. Then implement the binary search technique.

  • Step 4 − Return the position of the first set bit obtained as the result.

Example (C Program)

Here is the C program implementation of the above written algorithm to obtain the Position of the leftmost set bit in a given binary string where all 1s appear at the end.

#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, mid, res = -1;
   while (left <= right) {
      mid = (left + right) / 2;
      if (isBitSet(str, mid)) {
         res = mid;
         right = mid - 1;
      }
      else {
         left = mid + 1;
      }
   }
   return res;
}
int main(){
   char str[] = "01111";
   int l = strlen(str);
   printf("%d
", findFirstBit(str, l)); return 0; }

Output

On execution, it will produce the following output −

1

Conclusion

Likewise, we can implement a program to obtain the Position of leftmost set bit in given binary string where all 1s appear at end. The challenge of obtaining the position of the leftmost set bit in a given binary string where all 1s appear at the end is resolved in this article. Here C programming code as well as the methodology and the algorithm to obtain the Position of the leftmost set bit in a given binary string where all 1s appear at the end are provided.

Updated on: 31-Oct-2023

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements