Highest power of 2 that divides a number represented in binary | C ++ implementation of the approach ; Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int highestPower ( string str , int len ) { int ans = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) ans ++ ; else break ; } return ans ; } int main ( ) { string str = "100100" ; int len = str . length ( ) ; cout << highestPower ( str , len ) ; return 0 ; }