# Highest power of 2 that divides a number represented in binary

Given a 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

2^{2}= 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++

`// 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 number ` `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 code ` `int` `main() ` `{ ` ` ` `string str = ` `"100100"` `; ` ` ` `int` `len = str.length(); ` ` ` `cout << highestPower(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the highest power of 2 ` `// which divides the given binary number ` `static` `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 code ` `public` `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. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the highest power of 2 ` `# which divides the given binary number ` `def` `highestPower(` `str` `, lenngth): ` ` ` ` ` `# 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` `(lenngth` `-` `1` `,` `-` `1` `,` `-` `1` `): ` ` ` `if` `(` `str` `[i] ` `=` `=` `'0'` `): ` ` ` `ans` `+` `=` `1` `; ` ` ` `else` `: ` ` ` `break` `; ` ` ` `return` `ans; ` ` ` `# Driver code ` `def` `main(): ` ` ` `str` `=` `"100100"` `; ` ` ` `lenngth ` `=` `len` `(` `str` `); ` ` ` `print` `(highestPower(` `str` `, lenngth)); ` ` ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `main() ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the highest power of 2 ` `// which divides the given binary number ` `static` `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 code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"100100"` `; ` ` ` `int` `len = str.Length; ` ` ` `Console.WriteLine(highestPower(str, len)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the highest power of 2 ` `// which divides the given binary number ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Highest power of two that divides a given number
- Highest power of a number that divides other number
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Highest power of 2 less than or equal to given number
- Find maximum power of a number that divides a factorial
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Highest power of 2 less than or equal to given Integer
- Highest and Smallest power of K less than and greater than equal to N respectively
- Check in binary array the number represented by a subarray is odd or even
- Modulo power for large numbers represented as strings
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find two co-prime integers such that the first divides A and the second divides B
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Count of prime digits of a Number which divides the number
- Check if given number is a power of d where d is a power of 2
- Find the missing element in an array of integers represented in binary format
- Minimum value that divides one number and divisible by other
- Check if the sum of digits of a number N divides it
- Find the Smallest number that divides X^X
- Largest number that divides x and is co-prime with y

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.