

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer
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.
Example
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)); } }
Output
2
- 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.
- Program to find length of longest consecutive path of a binary tree in python
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Find longest sequence of 1’s in binary representation with one flip in C++
- Program to find length of longest consecutive sequence in Python
- Find length of the longest consecutive path from a given starting characters in C++
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive sublist with unique elements in Python
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Program to find length of longest arithmetic subsequence of a given list in Python
- Program to find longest consecutive run of 1s in binary form of n in Python
- Program to find length of longest sublist with given condition in Python
Advertisements