Check whether the number has only first and last bits set | C # program to check whether the number has only first and last bits set ; function to check whether ' n ' is a power of 2 or not ; function to check whether the number has only first and last bits set ; Driver program to test above
using System ; class GFG { static bool powerOfTwo ( uint n ) { return ( ( n & n - 1 ) == 0 ) ; } static bool onlyFirstAndLastAreSet ( uint n ) { if ( n == 1 ) return true ; return powerOfTwo ( n - 1 ) ; } public static void Main ( ) { uint n = ( uint ) 9 ; if ( onlyFirstAndLastAreSet ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }