Check if bitwise AND of any subset is power of two | Python3 Program to check if Bitwise AND of any subset is power of two ; Check for power of 2 or not ; Check if there exist a subset whose bitwise AND is power of 2. ; if there is only one element in the set . ; Finding a number with all bit sets . ; check all the positions at which the bit is set . ; include all those elements whose i - th bit is set ; check for the set contains elements make a power of 2 or not ; Driver Program
NUM_BITS = 32 NEW_LINE def isPowerOf2 ( num ) : NEW_LINE INDENT return ( num and ( num & ( num - 1 ) ) == 0 ) NEW_LINE DEDENT def checkSubsequence ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return isPowerOf2 ( arr [ 0 ] ) NEW_LINE DEDENT total = 0 NEW_LINE for i in range ( 0 , NUM_BITS ) : NEW_LINE INDENT total = total | ( 1 << i ) NEW_LINE DEDENT for i in range ( 0 , NUM_BITS ) : NEW_LINE INDENT ans = total NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT ans = ans & arr [ j ] NEW_LINE DEDENT DEDENT if ( isPowerOf2 ( ans ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 12 , 13 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkSubsequence ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT