Highest power of 2 less than or equal to given number | JavaScript program to find highest power of 2 smaller than or equal to n . ; If i is a power of 2 ; Driver code
function highestPowerof2 ( n ) { let res = 0 ; for ( let i = n ; i >= 1 ; i -- ) { if ( ( i & ( i - 1 ) ) == 0 ) { res = i ; break ; } } return res ; } let n = 10 ; document . write ( highestPowerof2 ( n ) ) ;