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

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);

`}`

$ 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.**

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

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