We have a number N. Determine the length of longest consecutive 0’s in its binary representation.

**Examples:**

Input : N = 14 Output : 1 Binary representation of 14 is 1110. There is only one 0 in the binary representation. Input : N = 9 Output : 2

A **simple approach** is to traverse through all bits and keep track of maximum number of consecutive 0s.

## C++

`// C++ code to determine Length of ` `// longest consecutive zeroes in the ` `// binary representation of a number. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `maxZeros(` `int` `N) ` `{ ` ` ` `// variable to store the length of ` ` ` `// longest consecutive 0's ` ` ` `int` `maxm = -1; ` ` ` ` ` `// to temporary store the consecutive 0's ` ` ` `int` `cnt = 0; ` ` ` ` ` `while` `(N) { ` ` ` `if` `(!(N & 1)) { ` ` ` `cnt++; ` ` ` `N >>= 1; ` ` ` `maxm = max(maxm, cnt); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `maxm = max(maxm, cnt); ` ` ` `cnt = 0; ` ` ` `N >>= 1; ` ` ` `} ` ` ` `} ` ` ` `return` `maxm; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 14; ` ` ` `cout << maxZeros(N) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to determine Length of ` `// longest consecutive zeroes in the ` `// binary representation of a number. ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `int` `maxZeros(` `int` `N) ` ` ` `{ ` ` ` `// variable to store the length of ` ` ` `// longest consecutive 0's ` ` ` `int` `maxm = -` `1` `; ` ` ` ` ` `// to temporary store the consecutive 0's ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `while` `(N != ` `0` `) { ` ` ` `if` `((N & ` `1` `) == ` `0` `) { ` ` ` `cnt++; ` ` ` `N >>= ` `1` `; ` ` ` `maxm = Math.max(maxm, cnt); ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `maxm = Math.max(maxm, cnt); ` ` ` `cnt = ` `0` `; ` ` ` `N >>= ` `1` `; ` ` ` `} ` ` ` `} ` ` ` `return` `maxm; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `14` `; ` ` ` `System.out.println(maxZeros(N)); ` ` ` ` ` `} ` ` ` `// This Code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to determine Length of ` `# longest consecutive zeroes in the ` `# binary representation of a number. ` `def` `maxZeros(N): ` ` ` ` ` `# variable to store the length ` ` ` `# of longest consecutive 0's ` ` ` `maxm ` `=` `-` `1` ` ` ` ` `# to temporary store the ` ` ` `# consecutive 0's ` ` ` `cnt ` `=` `0` ` ` `while` `(N): ` ` ` `if` `(` `not` `(N & ` `1` `)): ` ` ` `cnt ` `+` `=` `1` ` ` `N >>` `=` `1` ` ` `maxm ` `=` `max` `(maxm,cnt) ` ` ` `else` `: ` ` ` `maxm ` `=` `max` `(maxm,cnt) ` ` ` `cnt ` `=` `0` ` ` `N >>` `=` `1` ` ` ` ` `return` `maxm ` ` ` `# Driver Code ` `N ` `=` `14` `print` `(maxZeros(N)) ` ` ` `# This code is written by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# code to determine Length of ` `// longest consecutive zeroes in the ` `// binary representation of a number. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `maxZeros(` `int` `N) ` `{ ` ` ` `// variable to store the length ` ` ` `// of longest consecutive 0's ` ` ` `int` `maxm = -1; ` ` ` ` ` `// to temporary store the ` ` ` `// consecutive 0's ` ` ` `int` `cnt = 0; ` ` ` ` ` `while` `(N != 0) ` ` ` `{ ` ` ` `if` `((N & 1) == 0 ) ` ` ` `{ ` ` ` `cnt++; ` ` ` `N >>= 1; ` ` ` `maxm = Math.Max(maxm, cnt); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `maxm = Math.Max(maxm, cnt); ` ` ` `cnt = 0; ` ` ` `N >>= 1; ` ` ` `} ` ` ` `} ` ` ` `return` `maxm; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 14; ` ` ` `Console.WriteLine(maxZeros(N)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to determine Length of ` `// longest consecutive zeroes in the ` `// binary representation of a number. ` `function` `maxZeros(` `$N` `) ` `{ ` ` ` `// variable to store the length ` ` ` `// of longest consecutive 0's ` ` ` `$maxm` `= -1; ` ` ` ` ` `// to temporary store the ` ` ` `// of consecutive 0's ` ` ` `$cnt` `= 0; ` ` ` ` ` `while` `(` `$N` `) ` ` ` `{ ` ` ` `if` `(!(` `$N` `& 1)) ` ` ` `{ ` ` ` `$cnt` `++; ` ` ` `$N` `>>= 1; ` ` ` `$maxm` `= max(` `$maxm` `, ` `$cnt` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `$maxm` `= max(` `$maxm` `, ` `$cnt` `); ` ` ` `$cnt` `= 0; ` ` ` `$N` `>>= 1; ` ` ` `} ` ` ` `} ` ` ` `return` `$maxm` `; ` `} ` ` ` `// Driver code ` `$N` `= 14; ` `echo` `(maxZeros(` `$N` `)); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

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 the Longest Consecutive 1s in Binary Representation
- Find consecutive 1s of length >= n in binary representation of a number
- Maximum number of consecutive 1's in binary representation of all the array elements
- 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 longest sequence of 1's in binary representation with one flip
- Number of leading zeros in binary representation of a given number
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Count number of trailing zeros in Binary representation of a number using Bitset
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Binary representation of a given number
- Check if binary representation of a number is palindrome
- Check if binary representation of a given number and its complement are anagram
- Find the n-th number whose binary representation is a palindrome
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Check if actual binary representation of a number is palindrome
- Prime Number of Set Bits in Binary Representation | Set 1
- Next greater number than N with exactly one bit different in binary representation of N

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.