Given a positive integer n, check whether only the first and last bits are set in the binary representation of n. Print ‘Yes’ or ‘No’.

**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.

We have already discussed a solution here.

In this post, a simpler solution is discussed.

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java to check whether ` `// the number has only ` `// first and last bits set ` ` ` `class` `GFG ` `{ ` `// function to check whether ` `// the number has only ` `// first and last bits set ` `static` `boolean` `onlyFirstAndLastAreSet(` `int` `n) ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `true` `; ` ` ` `if` `(n == ` `2` `) ` ` ` `return` `false` `; ` ` ` `return` `(((n - ` `1` `) & ` ` ` `(n - ` `2` `)) == ` `0` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `9` `; ` ` ` `if` `(onlyFirstAndLastAreSet(n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by Smitha ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 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 ` `def` `onlyFirstAndLastAreSet(n): ` ` ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `True` ` ` `if` `(n ` `=` `=` `2` `): ` ` ` `return` `False` ` ` ` ` `return` `(((n ` `-` `1` `) & ` ` ` `(n ` `-` `2` `)) ` `=` `=` `0` `) ` ` ` `# Driver Code ` `n ` `=` `9` `if` `(onlyFirstAndLastAreSet(n)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed ` `# by Smitha ` |

*chevron_right*

*filter_none*

## C#

`// C# to check whether ` `// the number has only ` `// first and last bits set ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// function to check whether ` `// the number has only ` `// first and last bits set ` `static` `bool` `onlyFirstAndLastAreSet(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `true` `; ` ` ` `if` `(n == 2) ` ` ` `return` `false` `; ` ` ` `return` `(((n - 1) & ` ` ` `(n - 2)) == 0); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 9; ` ` ` `if` `(onlyFirstAndLastAreSet(n)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed ` `// by Smitha ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `onlyFirstAndLastAreSet(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `true; ` ` ` `if` `(` `$n` `== 2) ` ` ` `return` `false; ` ` ` `return` `(((` `$n` `- 1) & ` ` ` `(` `$n` `- 2)) == 0); ` `} ` ` ` `// Driver Code ` `$n` `= 9; ` `if` `(onlyFirstAndLastAreSet(` `$n` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed ` `// by Smitha ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Check if bits of a number has count of consecutive set bits in increasing order
- Check if all bits of a number are set
- Check whether the number has only first and last bits set
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has same number of set and unset bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Toggle bits of a number except first and last bits
- Check whether all the bits are set in the given range
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check whether bits are in alternate pattern in the given range | Set-2
- Check if bits in range L to R of two numbers are complement of each other or not
- Check whether bits are in alternate pattern in the given range
- Maximize a given unsigned number number by swapping bits at it's extreme positions.