Given a number n, find length of the longest consecutive 1s in its binary representation.

**Examples :**

Input : n = 14 Output : 3 The binary representation of 14 is1110. Input : n = 222 Output : 4 The binary representation of 222 is 11011110.

**Naive Approach:** One simple way would be to simply loop over the bits, and keep track of the number of consecutive set bits, and the maximum that this value has reached. In this approach, we need to convert it to binary (base-2) representation and then find and print the result.

**Using Bit Magic: ** The idea is based on the concept that if we AND a bit sequence with a shifted version of itself, we’re effectively removing the trailing 1 from every sequence of consecutive 1s.

11101111 (x) & 11011110 (x << 1) ---------- 11001110 (x & (x << 1)) ^ ^ | | trailing 1 removed

So the operation x = (x & (x << 1)) reduces length of every sequence of 1s by one in binary representation of x. If we keep doing this operation in a loop, we end up with x = 0. The number of iterations required to reach 0 is actually length of the longest consecutive sequence of 1s.

## C++

`// C++ program to find length of the longest ` `// consecutive 1s in binary reprsentation of ` `// a number. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `maxConsecutiveOnes(` `int` `x) ` `{ ` ` ` `// Initialize result ` ` ` `int` `count = 0; ` ` ` ` ` `// Count the number of iterations to ` ` ` `// reach x = 0. ` ` ` `while` `(x!=0) ` ` ` `{ ` ` ` `// This operation reduces length ` ` ` `// of every sequence of 1s by one. ` ` ` `x = (x & (x << 1)); ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `cout << maxConsecutiveOnes(14) << endl; ` ` ` `cout << maxConsecutiveOnes(222) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find length of the longest ` `// consecutive 1s in binary reprsentation of ` `// a number. ` `class` `MaxConsecutiveOnes ` `{ ` ` ` `private` `static` `int` `maxConsecutiveOnes(` `int` `x) ` ` ` `{ ` ` ` `// Initialize result ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Count the number of iterations to ` ` ` `// reach x = 0. ` ` ` `while` `(x!=` `0` `) ` ` ` `{ ` ` ` `// This operation reduces length ` ` ` `// of every sequence of 1s by one. ` ` ` `x = (x & (x << ` `1` `)); ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String strings[]) ` ` ` `{ ` ` ` `System.out.println(maxConsecutiveOnes(` `14` `)); ` ` ` `System.out.println(maxConsecutiveOnes(` `222` `)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# length of the longest ` `# consecutive 1s in ` `# binary reprsentation of ` `# a number. ` ` ` `def` `maxConsecutiveOnes(x): ` ` ` ` ` `# Initialize result ` ` ` `count ` `=` `0` ` ` ` ` `# Count the number of iterations to ` ` ` `# reach x = 0. ` ` ` `while` `(x!` `=` `0` `): ` ` ` ` ` `# This operation reduces length ` ` ` `# of every sequence of 1s by one. ` ` ` `x ` `=` `(x & (x << ` `1` `)) ` ` ` ` ` `count` `=` `count` `+` `1` ` ` ` ` `return` `count ` ` ` `# Driver code ` ` ` `print` `(maxConsecutiveOnes(` `14` `)) ` `print` `(maxConsecutiveOnes(` `222` `)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find length of the ` `// longest consecutive 1s in binary ` `// reprsentation of a number. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find length of the ` ` ` `// longest consecutive 1s in binary ` ` ` `// reprsentation of a number ` ` ` `private` `static` `int` `maxConsecutiveOnes(` `int` `x) ` ` ` `{ ` ` ` ` ` `// Initialize result ` ` ` `int` `count = 0; ` ` ` ` ` `// Count the number of iterations ` ` ` `// to reach x = 0. ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` ` ` `// This operation reduces length ` ` ` `// of every sequence of 1s by one. ` ` ` `x = (x & (x << 1)); ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `Console.WriteLine(maxConsecutiveOnes(14)); ` ` ` `Console.Write(maxConsecutiveOnes(222)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find length ` `// of the longest consecutive ` `// 1s in binary reprsentation of ` `// a number. ` ` ` `function` `maxConsecutiveOnes(` `$x` `) ` `{ ` ` ` `// Initialize result ` ` ` `$count` `= 0; ` ` ` ` ` `// Count the number of ` ` ` `// iterations to reach x = 0. ` ` ` `while` `(` `$x` `!= 0) ` ` ` `{ ` ` ` `// This operation reduces ` ` ` `// length of every sequence ` ` ` `// of 1s by one. ` ` ` `$x` `= (` `$x` `& (` `$x` `<< 1)); ` ` ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `echo` `maxConsecutiveOnes(14), ` `"\n"` `; ` `echo` `maxConsecutiveOnes(222), ` `"\n"` `; ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3 4

This article is contributed by **Pankaj Kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest consecutive zeroes in the binary representation of a number.
- Find consecutive 1s of length >= n in binary representation of a number
- Length of second longest sequence of consecutive 1s in a binary array
- 1 to n bit numbers with no consecutive 1s in binary representation.
- 1 to n bit numbers with no consecutive 1s in binary representation
- Find next greater element with no consecutive 1 in it's binary representation
- Maximum number of consecutive 1's in binary representation of all the array elements
- Find longest sequence of 1's in binary representation with one flip
- Longest common substring in binary representation of two numbers
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Length of longest connected 1’s in a Binary Grid
- Binary representation of a given number
- Check if binary representation of a number is palindrome
- XOR counts of 0s and 1s in binary representation
- Maximum 0's between two immediate 1's in binary representation
- Decimal representation of given binary string is divisible by 5 or not
- Binary representation of next number
- Check if binary representation of a given number and its complement are anagram
- Find the n-th number whose binary representation is a palindrome