Check whether the number has only first and last bits set | Set 2 - GeeksforGeeks

08 Apr, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 setbool onlyFirstAndLastAreSet(unsigned int n){    if (n == 1)        return true;    if (n == 2)        return false;    return (((n - 1) & (n - 2)) == 0);} // Driver program to test aboveint main(){    unsigned int n = 9;    if (onlyFirstAndLastAreSet(n))        cout << "Yes";    else        cout << "No";     return 0;}
// 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 setstatic boolean onlyFirstAndLastAreSet(int n){    if (n == 1)        return true;    if (n == 2)        return false;    return (((n - 1) &             (n - 2)) == 0);} // Driver Codepublic 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
# 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 setdef onlyFirstAndLastAreSet(n):     if (n == 1):        return True    if (n == 2):        return False     return (((n - 1) &             (n - 2)) == 0) # Driver Coden = 9if (onlyFirstAndLastAreSet(n)):    print("Yes")else:    print("No")     # This code is contributed# by Smitha
// C# to check whether// the number has only// first and last bits setusing System; class GFG{// function to check whether// the number has only// first and last bits setstatic bool onlyFirstAndLastAreSet(int n){    if (n == 1)        return true;    if (n == 2)        return false;    return (((n - 1) &             (n - 2)) == 0);} // Driver Codepublic static void Main(){    int n = 9;    if (onlyFirstAndLastAreSet(n))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed// by Smitha
<?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 setfunction 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?>
<script> // javascript to check whether// the number has only// first and last bits set // function to check whether// the number has only// first and last bits setfunction onlyFirstAndLastAreSet(n){    if (n == 1)        return true;    if (n == 2)        return false;    return (((n - 1) &             (n - 2)) == 0);} // Driver Code var n = 9;if (onlyFirstAndLastAreSet(n))    document.write("Yes");else    document.write("No"); // This code contributed by shikhasingrajput </script>
Yes
 
Smitha Dinesh Semwal
shikhasingrajput
Binary Search
Bit Algorithms
Bit Magic
Misc
Misc
Bit Magic
Misc
Binary Search
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Add two numbers without using arithmetic operators
Binary representation of a given number
Program to find whether a given number is power of 2
Top 10 algorithms in Interview Questions
Find all factors of a natural number | Set 1
vector::push_back() and vector::pop_back() in C++ STL
How to write Regular Expressions?
fgets() and gets() in C language