C# program to find the length of the Longest Consecutive 1's in Binary Representation of a given integer

Finding the longest consecutive 1's in the binary representation of a number is a common bit manipulation problem. The key is to use the bitwise AND operation combined with left shift to eliminate consecutive 1's one group at a time.

Syntax

The core operation uses bitwise AND with left shift −

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

This operation removes one group of consecutive 1's in each iteration −

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

How It Works

The algorithm works by repeatedly applying the bitwise AND operation between the number and its left-shifted version. Each iteration removes the shortest group of consecutive 1's, and we count how many iterations it takes until all 1's are eliminated.

Binary Manipulation Process (150 = 10010110) Original: 150 = 1 00 11 0 11 0 Step 1: Remove shortest group (length 1) Step 2: Remove next shortest group (length 2) Result: 2 groups removed = Length of longest consecutive 1's is 2 Each iteration removes one group until all 1's are eliminated

Using Bitwise Operations

Example

using System;

class Demo {
   private static int findConsecutive(int i) {
      int count = 0;
      while (i != 0) {
         i = (i & (i << 1));
         count++;
      }
      return count;
   }

   public static void Main() {
      // Binary of 150 is 10010110
      Console.WriteLine("Longest consecutive 1's in 150: " + findConsecutive(150));
      
      // Binary of 222 is 11011110  
      Console.WriteLine("Longest consecutive 1's in 222: " + findConsecutive(222));
      
      // Binary of 31 is 11111
      Console.WriteLine("Longest consecutive 1's in 31: " + findConsecutive(31));
   }
}

The output of the above code is −

Longest consecutive 1's in 150: 2
Longest consecutive 1's in 222: 4
Longest consecutive 1's in 31: 5

Alternative Approach Using String Conversion

Example

using System;

class Program {
   private static int findConsecutiveString(int num) {
      string binary = Convert.ToString(num, 2);
      int maxCount = 0;
      int currentCount = 0;
      
      foreach (char bit in binary) {
         if (bit == '1') {
            currentCount++;
            maxCount = Math.Max(maxCount, currentCount);
         } else {
            currentCount = 0;
         }
      }
      return maxCount;
   }

   public static void Main() {
      int number = 150;
      string binary = Convert.ToString(number, 2);
      Console.WriteLine("Number: " + number + ", Binary: " + binary);
      Console.WriteLine("Longest consecutive 1's: " + findConsecutiveString(number));
   }
}

The output of the above code is −

Number: 150, Binary: 10010110
Longest consecutive 1's: 2

Comparison

Approach Time Complexity Space Complexity Advantage
Bitwise Operations O(log n) O(1) Memory efficient, faster execution
String Conversion O(log n) O(log n) More readable and easier to understand

Conclusion

Finding the longest consecutive 1's can be solved efficiently using bitwise operations where each iteration removes one group of consecutive 1's. The bitwise approach is more memory-efficient, while string conversion offers better readability for understanding the algorithm.

Updated on: 2026-03-17T07:04:35+05:30

361 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements