Check whether the number has only first and last bits set | Set 2 | C ++ to check whether the number has only first and last bits set ; function to check whether the number has only first and last bits set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool onlyFirstAndLastAreSet ( unsigned int n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) ; } int main ( ) { unsigned int n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }