- Related Questions & Answers
- Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Find longest sequence of 1’s in binary representation with one flip in C++
- Program to find length of longest consecutive path of a binary tree in python
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Find length of the longest consecutive path from a given starting characters in C++
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Program to find length of longest consecutive sequence in Python
- Given a sorted array of 0’s and 1’s, find the transition point of the array in C++
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Maximum length of segments of 0’s and 1’s in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

To fetch the consecutive 1’s, use the Bitwise Left Shift Operator. Here is our decimal number.

i = (i & (i << 1));

Loop the above until the value of I is 0 and fetch the length using a variable; count here.

while (i != 0) { i = (i & (i << 1)); count++; }

The example we have taken here is 150.

The binary for 150 is 10010110. Therefore, we have two consecutive one’s.

using System; class Demo { private static int findConsecutive(int i) { int count = 0; while (i != 0) { i = (i & (i < 1)); count++; } return count; } // Driver code public static void Main() { // Binary or 150 is 10010110 Console.WriteLine(findConsecutive(150)); } }

2

Advertisements