Length of longest consecutive zeroes in the binary representation of a number.

27 May, 2021
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 the maximum number of consecutive 0s. 
C++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int N = 14;    cout << maxZeros(N) << endl;    return 0;}
// 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}
# 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 CodeN = 14print(maxZeros(N)) # This code is written by Shrikant13
// 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 codepublic static void Main(){    int N = 14;    Console.WriteLine(maxZeros(N));}} // This code is contributed// by anuj_67
<?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?>
<script>    // Javascript 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        let maxm = -1;         // to temporary store the        // consecutive 0's        let 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;    }         let N = 14;    document.write(maxZeros(N));     </script>
1
 
shrikanth13
ankthon
vt_m
Shivi_Aggarwal
suresh07
Technical Scripter 2018
Bit Magic
Technical Scripter
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Google Online Challenge 2020
Calculate XOR from 1 to n.
Find two numbers from their sum and XOR
Calculate square of a number without using *, / and pow()
Reverse actual bits of the given number
Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
Find XOR of two number without using XOR operator
Unique element in an array where all elements occur k times except one