- Related Questions & Answers
- C# 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
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- 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 find length of longest consecutive sequence in Python
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Given a sorted array of 0’s and 1’s, find the transition point of the array in C++
- 1’s and 2’s complement of a Binary Number?
- Maximum 0’s between two immediate 1’s in binary representation 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

The length of the longest consecutive 1’s in the binary representation of a given integer involves the length of the longest series of 1’s that occur together. An example of this is given as follows −

Number = 13 Binary representation = 1101

The length of the longest consecutive 1’s in binary representation of 13 = 2

A program that demonstrates this is given as follows −

public class Example { public static void main(String strings[]) { int num = 55; int n = num; int count = 0; while (num!=0) { num = (num & (num << 1)); count++; } System.out.println("The length of the longest consecutive 1's in binary representation of " + n + " is: " + count); } }

The length of the longest consecutive 1's in binary representation of 55 is: 3

Now let us understand the above program.

The value of the number is defined. Then, length of the longest consecutive 1's in binary representation of the number is found using a while loop and stored in count variable. Finally, the value of count is displayed. The code snippet that demonstrates this is given as follows −

int num = 55; int n = num; int count = 0; while (num!=0) { num = (num & (num << 1)); count++; } System.out.println("The length of the longest consecutive 1's in binary representation of " + n + " is: " + count);

Advertisements