text: Given a HUGE number check if it 's a power of two. | returns 1 when str is power of 2 return 0 when str is not a power of 2 ; sum stores the intermediate dividend while dividing . ; if the input is "1" then return 0 because 2 ^ k = 1 where k >= 1 and here k = 0 ; Divide the number until it gets reduced to 1 if we are successfully able to reduce the number to 1 it means input string is power of two if in between an odd number appears at the end it means string is not divisible by two hence not a power of 2. ; if the last digit is odd then string is not divisible by 2 hence not a power of two return 0. ; divide the whole string by 2. i is used to track index in current number . j is used to track index for next iteration . ; if num < 2 then we have to take another digit to the right of A [ i ] to make it bigger than A [ i ] . E . g . 214 / 2 -- > 107 ; if it 's not the first index. E.g 214  then we have to include 0. ; for eg . "124" we will not write 064 so if it is the first index just ignore ; After every division by 2 the length of string is changed . ; if the string reaches to 1 then the str is a power of 2. ; Driver code .
code: < ? php function isPowerOf2 ( $ str ) { $ len_str = strlen ( $ str ) ; $ num = 0 ; if ( $ len_str == 1 && $ str [ $ len_str - 1 ] == '1' ) return 0 ; while ( $ len_str != 1 $ str [ $ len_str - 1 ] != '1' ) { if ( ord ( $ str [ $ len_str - 1 ] - '0' ) % 2 == 1 ) return 0 ; $ j = 0 ; for ( $ i = 0 ; $ i < $ len_str ; $ i ++ ) { $ num = $ num * 10 + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ; if ( $ num < 2 ) { if ( $ i != 0 ) $ str [ $ j ++ ] = '0' ; continue ; } $ str [ $ j ++ ] = chr ( ( int ) ( $ num / 2 ) + ord ( '0' ) ) ; $ num = ( $ num ) - ( int ) ( $ num / 2 ) * 2 ; } $ len_str = $ j ; } return 1 ; } $ str1 = "124684622466842024680246842024662202000002" ; $ str2 = "1" ; $ str3 = "128" ; print ( isPowerOf2 ( $ str1 ) . " " . isPowerOf2 ( $ str2 ) . " " ? > ? >