C Program to Find the Position of 1-bits

This is a C Program to find the position of string of 1-bits in a number for a given length.

Problem Description

This C Program finds the position of string of 1-bits in a number for a given length.

Problem Solution

Take input from the user and finds string position as shown in the program below.

Program/Source Code

Here is source code of the C Program to find the position of string of 1-bits in a number for a given length. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find the Position of String of 1-bits in a Number 
 * for a given Length
 */
#include <stdio.h>
 
void main()
{
    int n, len, pos = 0, i = 0, count = 0;
 
    printf("**Finding the position of 1-bits in a number for given length**\n");
    printf("enter a number\n");
    scanf("%d", &n);
    printf("enter the length\n");
    scanf("%d", &len);
    while (i <= 32)
    {
        if ((n & 1) ==  1)    //checking whether there is a 1-bit in the current position
        {
            count++;//counting the consecutive 1's in the integer
            pos = i;
            if (count ==  len)    //checking whether the length matches
            {
                break;
            }
        }
        if ((n & 1) ==  0)
        {
            count = 0;
        }
        n = n>>1;
        i++;
    }
    printf("the position of 1 in the string : %d\n", pos);
}
Program Explanation

In this C Program, we are reading the number and length using ‘n’ and ‘len’ variables respectively. Using while loop assign the position of strings of 1-bits in a number for a given length. If condition statement is used to check that there is a 1-bit in the current position. If the condition is true, then it will execute the statement for counting the consecutive 1’s in the integer.

advertisement
advertisement

Another if condition statement is used for checking whether the length matches. Another if condition statement is used to check whether there is a 1-bit in the starting position. If the condition is true then execute the statement.

Using Binary Right shift operator, the left operand’s value is moved right by the number of bits specified by the right operands and assign the value to n variable then count the consecutive 1’s in the integer. Print the position of string of 1-bits in a number for a given length.

Runtime Test Cases
 
$ cc bit7.c
$ a.out
**Finding the position of 1-bits in a number for given length**
enter a number
10000
enter the length
3
the position of 1 in the string : 10
$ a.out
enter a number
700
enter the length
4
the position of 1 in the string : 5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.