text: Highest power of 2 less than or equal to given Integer | C # implementation of the above approach ; Function to return the lowest power of 2 close to given positive number ; Floor function is used to determine the value close to the number ; Function to return the lowest power of 2 close to given negative number ; Ceil function is used for negative numbers as - 1 > - 4. It would be opposite to positive numbers where 1 < 4 ; Function to find the highest power of 2 ; To check if the given number is positive or negative ; If the number is negative , then the ceil of the positive number is calculated and negative sign is added ; Driver code
code: using System ; class GFG { static int powOfPositive ( int n ) { int pos = ( int ) Math . Floor ( ( Math . Log ( n ) / Math . Log ( 2 ) ) ) ; return ( int ) Math . Pow ( 2 , pos ) ; } static int powOfNegative ( int n ) { int pos = ( int ) Math . Ceiling ( ( Math . Log ( n ) / Math . Log ( 2 ) ) ) ; return ( int ) ( - 1 * Math . Pow ( 2 , pos ) ) ; } static void highestPowerOf2 ( int n ) { if ( n > 0 ) { Console . WriteLine ( powOfPositive ( n ) ) ; } else { n = - n ; Console . WriteLine ( powOfNegative ( n ) ) ; } } public static void Main ( ) { int n = - 24 ; highestPowerOf2 ( n ) ; } }