- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

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

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 −

## Example

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); } }

## Output

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);

- Related Articles
- 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.
- Program to find length of longest consecutive path of a binary tree in python
- Find longest sequence of 1’s in binary representation with one flip in C++
- Program to find longest consecutive run of 1 in binary form of a number in C++
- Find consecutive 1s of length >= n in binary representation of a number 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++
- 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 longest consecutive run of 1s in binary form of n in Python
- Program to find length of longest consecutive sublist with unique elements in Python
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Program to find length of longest arithmetic subsequence of a given list in Python

Advertisements