# Check whether the number has only first and last bits set

Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n.

Examples:

```Input : 9
Output : Yes
(9)10 = (1001)2, only the first and
last bits are set.

Input : 15
Output : No
(15)10 = (1111)2, except first and last
there are other bits also which are set.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Following are the steps:

1. If n == 1, return “Yes”.
2. Else check whether n-1 is a power of 2. Refer this post.

## C++

 `// C++ to check whether the number has only  ` `// first and last bits set ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// function to check whether 'n' ` `// is a power of 2 or not ` `bool` `powerOfTwo(unsigned ``int` `n) ` `{ ` `    ``return` `(!(n & n-1)); ` `} ` ` `  `// function to check whether the number has only  ` `// first and last bits set ` `bool` `onlyFirstAndLastAreSet(unsigned ``int` `n) ` `{ ` `    ``if` `(n == 1) ` `        ``return` `true``; ` `    ``if` `(n == 2) ` `        ``return` `false``; ` `    ``return` `powerOfTwo(n-1);     ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``unsigned ``int` `n = 9; ` `     `  `    ``if` `(onlyFirstAndLastAreSet(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``;     ` `     `  `    ``return` `0; ` `}  `

## Java

 `// Java program to check whether the  ` `// number has only first and last  ` `// bits set ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `    ``// function to check whether 'n' ` `    ``// is a power of 2 or not ` `    ``static` `boolean` `powerOfTwo(``int` `n) ` `    ``{ ` `        ``return` `((n & n - ``1``) == ``0``); ` `    ``} ` ` `  `    ``// function to check whether the number has   ` `    ``// only first and last bits set ` `    ``static` `boolean` `onlyFirstAndLastAreSet(``int` `n) ` `    ``{ ` `        ``if` `(n == ``1``) ` `            ``return` `true``; ` `     `  `        ``return` `powerOfTwo(n-``1``);  ` `    ``} ` ` `  `    ``// Driver program to test above ` `    ``public` `static` `void` `main (String[] args) { ` `        `  `       ``int` `n = Integer.parseUnsignedInt(``"9"``); ` `     `  `        ``if` `(onlyFirstAndLastAreSet(n)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``);  ` `    ``} ` `}  ` ` `  `/* This code is contributed by Mr. Somesh Awasthi */`

## Python3

 `# Python3 program to check whether number  ` `# has only first and last bits set ` ` `  `# function to check whether 'n' ` `# is a power of 2 or not ` `def` `powerOfTwo (n): ` `    ``return` `(``not``(n & n``-``1``)) ` ` `  ` `  `# function to check whether number   ` `# has only first and last bits set ` `def` `onlyFirstAndLastAreSet (n): ` ` `  `    ``if` `(n ``=``=` `1``): ` `        ``return` `True` `    ``return` `powerOfTwo (n``-``1``) ` ` `  `# Driver program to test above ` `n ``=` `9` `if` `(onlyFirstAndLastAreSet (n)): ` `    ``print``(``'Yes'``) ` `else``: ` `    ``print``(``'No'``) ` ` `  `# This code is contributed by Shariq Raza `

## C#

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

## PHP

 ` `

Output:

```Yes
```

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : Sach_Code

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.