Highest power of 2 that divides a number represented in binary - GeeksforGeeks

16 Nov, 2021
Given binary string str, the task is to find the largest power of 2 that divides the decimal equivalent of the given binary number.
Examples:  
Input: str = “100100” Output: 2 22 = 4 is the highest power of 2 that divides 36 (100100).Input: str = “10010” Output: 1  
Approach: Starting from the right, count the number of 0s in the binary representation which is the highest power of 2 which will divide the number.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the highest power of 2// which divides the given binary numberint highestPower(string str, int len){     // To store the highest required power of 2    int ans = 0;     // Counting number of consecutive zeros    // from the end in the given binary string    for (int i = len - 1; i >= 0; i--) {        if (str[i] == '0')            ans++;        else            break;    }     return ans;} // Driver codeint main(){    string str = "100100";    int len = str.length();    cout << highestPower(str, len);     return 0;}
// Java implementation of the approachclass GFG{     // Function to return the highest power of 2// which divides the given binary numberstatic int highestPower(String str, int len){     // To store the highest required power of 2    int ans = 0;     // Counting number of consecutive zeros    // from the end in the given binary string    for (int i = len - 1; i >= 0; i--)    {        if (str.charAt(i) == '0')            ans++;        else            break;    }     return ans;} // Driver codepublic static void main(String[] args){    String str = "100100";    int len = str.length();    System.out.println(highestPower(str, len));}} // This code is contributed by Code_Mech.
# Python3 implementation of the approach # Function to return the highest power of 2# which divides the given binary numberdef highestPower(str, length):     # To store the highest required power of 2    ans = 0;     # Counting number of consecutive zeros    # from the end in the given binary string    for i in range(length-1,-1,-1):        if (str[i] == '0'):            ans+=1;        else:            break;    return ans; # Driver codedef main():    str = "100100";    length = len(str);    print(highestPower(str, length)); if __name__ == '__main__':    main() # This code contributed by PrinciRaj1992
// C# implementation of the approachusing System;     class GFG{     // Function to return the highest power of 2// which divides the given binary numberstatic int highestPower(String str, int len){     // To store the highest required power of 2    int ans = 0;     // Counting number of consecutive zeros    // from the end in the given binary string    for (int i = len - 1; i >= 0; i--)    {        if (str[i] == '0')            ans++;        else            break;    }     return ans;} // Driver codepublic static void Main(String[] args){    String str = "100100";    int len = str.Length;    Console.WriteLine(highestPower(str, len));}} /* This code contributed by PrinciRaj1992 */
<?php// PHP implementation of the approach // Function to return the highest power of 2// which divides the given binary numberfunction highestPower($str, $len){     // To store the highest required power of 2    $ans = 0;     // Counting number of consecutive zeros    // from the end in the given binary string    for ($i = $len - 1; $i >= 0; $i--)    {        if ($str[$i] == '0')            $ans++;        else            break;    }     return $ans;} // Driver code$str = "100100";$len = strlen($str);echo highestPower($str, $len); // This code is contributed by Ryuga?>
<script>// Javascript implementation of the approach // Function to return the highest power of 2// which divides the given binary numberfunction highestPower(str, len){     // To store the highest required power of 2    let ans = 0;     // Counting number of consecutive zeros    // from the end in the given binary string    for (let i = len - 1; i >= 0; i--) {        if (str[i] == '0')            ans++;        else            break;    }     return ans;} // Driver code    let str = "100100";    let len = str.length;    document.write(highestPower(str, len));     </script>
2
Time Complexity: O(N)
Auxiliary Space: O(1)
ankthon
Code_Mech
princiraj1992
rishavmahato348
simmytarika5
surindertarika1234
rohan07
binary-representation
Number Divisibility
Bit Magic
Competitive Programming
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Add two numbers without using arithmetic operators
Josephus problem | Set 1 (A O(n) Solution)
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Fast I/O for Competitive Programming