Check if bitwise AND of any subset is power of two | Javascript 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
var NUM_BITS = 32 ; function isPowerOf2 ( num ) { return ( num && ! ( num & ( num - 1 ) ) ) ; } function checkSubsequence ( arr , n ) { if ( n == 1 ) return isPowerOf2 ( arr [ 0 ] ) ; var total = 0 ; for ( var i = 0 ; i < NUM_BITS ; i ++ ) total = total | ( 1 << i ) ; for ( var i = 0 ; i < NUM_BITS ; i ++ ) { var ans = total ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) ans = ans & arr [ j ] ; } if ( isPowerOf2 ( ans ) ) return true ; } return false ; } var arr = [ 12 , 13 , 7 ] ; var n = arr . length ; if ( checkSubsequence ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;