Open In App

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

Last Updated : 21 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Any number with first and last bits set are of the form (1+2^n):
    2^n     =>  10000… (n zeroes)
+ 1               +        1
 ————————————-
(1 + 2^n) => 10000….1 (n-1 zeroes)

For any given number ‘N’ of the form (1 + 2^n), we have: ((N-1) & (N-2)) = 0

    ((1 + 2^n) – 1) =>    (2^n)        => 10000…. (n zeroes) 
& ((1 + 2^n) – 2) => &((2^n) – 1) => 01111…. (n 1’s)
————————————————————–
                                                            00000….

For example: ‘9’ is of the form (1 + 2^3)
   (9)10 => (1001)2
   (8)10 =>    (1000)2
&(7)10 =>& (0111)2
———————–
                     (0000)2

Hence for any given number ‘N’, if ((N-1) & (N-2)) == 0, then we can say ‘N’ has only first and last bits set.

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;
}


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


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


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


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
?>


Javascript




<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 set
function 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>


Output: 

Yes

 

Time complexity: O(1)
Auxiliary space: O(1)



Similar Reads

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 PracticeFirst a
4 min read
Print numbers having first and last bits as the only set bits
Given a positive integer n. The problem is to print numbers in the range 1 to n having first and last bits as the only set bits.Examples: Input : n = 10 Output : 1 3 5 9 (1)10 = (1)2. (3)10 = (11)2. (5)10 = (101)2. (9)10 = (1001)2 Naive Approach: Print "1". Now for i = 3 to n, check if (i-1) is a Perfect power of two or not. If true then print i. C
10 min read
Check if bits of a number has count of consecutive set bits in increasing order
Given a integer n &gt; 0, the task is to find whether in the bit pattern of integer count of continuous 1's are in increasing from left to right. Examples : Input:19 Output:Yes Explanation: Bit-pattern of 19 = 10011, Counts of continuous 1's from left to right are 1, 2 which are in increasing order. Input : 183 Output : yes Explanation: Bit-pattern
11 min read
Toggle bits of a number except first and last bits
Given a number, the task is to toggle bits of the number except the first and the last bit.Examples: Input : 10 Output : 12 Binary representation:- 1 0 1 0 After toggling first and last : 1 1 0 0 Input : 9 Output : 15 Binary representation : 1 0 0 1 After toggling first and last : 1 1 1 1 Prerequisite: Find most significant set bit of a number Gene
8 min read
Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
Given two numbers x, y which denotes the number of set bits. Also given is a number C. The task is to print the number of ways in which we can form two numbers A and B such that A has x number of set bits and B has y number of set bits and A+B = C.Examples: Input: X = 1, Y = 1, C = 3 Output: 2 So two possible ways are (A = 2 and B = 1) and (A = 1 a
30 min read
Check if a number has same number of set and unset bits
Given a number N, the task is to check whether the count of the set and unset bits in the given number are same. Examples: Input: 12Output: Yes1100 is the binary representation of 12which has 2 set and 2 unset bits Input: 14Output: No Approach: Traverse in the binary representation of the given number, check if the leftmost bit is set or not using
5 min read
Check if a number has bits in alternate pattern | Set 1
Given an integer n &gt; 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101. Print "Yes" if it has an alternate pattern otherwise "No". Here alternate patterns can be like 0101 or 1010. Examples: Input : 15 Output : No Explanation: Binary representation of
8 min read
Check if a number has two adjacent set bits
Given a number you have to check whether there is pair of adjacent set bit or not.Examples : Input : N = 67 Output : Yes There is a pair of adjacent set bit The binary representation is 100011 Input : N = 5 Output : No Recommended PracticeNumbers having two adjacent set bitsTry It! A simple solution is to traverse all bits. For every set bit, check
3 min read
Check if a number has bits in alternate pattern | Set-2 O(1) Approach
Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate pattern means that the set and unset bits in n occur in alternate order. For example- 5 has an alternate pattern i.e. 101. Print “Yes” if it has an alternate pattern otherwise “No”. Note: 0 &lt; n.Exa
6 min read
Toggle first and last bits of a number
Given a number n, the task is to toggle only first and last bits of a numberExamples : Input : 10 Output : 3 Binary representation of 10 is 1010. After toggling first and last bits, we get 0011. Input : 15 Output : 6 Prerequisite : Find MSB of given number.1) Generate a number which contains first and last bit as set. We need to change all middle b
8 min read
Article Tags :
Practice Tags :