Check LSB and MSB Set

Last Updated : 21 Jul, 2026

Given a positive integer n, find whether the binary representation of n has only the most significant bit (MSB) and the least significant bit (LSB) set.

Examples: 

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

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

Try It Yourself
redirect icon

[Naive Approach] Binary Representation Traversal - O(log n) Time and O(log n) Space

The idea is to convert the given number into its binary representation and count the number of set bits. If exactly two bits are set, check whether they are the least significant bit (LSB) and the most significant bit (MSB). The special case n = 1 is also treated as valid since its only bit is both the MSB and LSB.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to check whether only the MSB and LSB are set.
bool areSet(int n)
{
    // Special case: 1 has only one bit, which is both MSB and LSB.
    if (n == 1)
        return true;

    vector<int> bits;

    // Store the binary representation of the number.
    while (n > 0)
    {
        bits.push_back(n % 2);
        n /= 2;
    }

    int cnt = 0;

    // Count the number of set bits.
    for (int bit : bits)
        cnt += bit;

    // Check whether only the first and last bits are set.
    return (cnt == 2 && bits[0] == 1 && bits.back() == 1);
}

int main()
{
    int n = 9;

    if (areSet(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.util.ArrayList;

public class GFG {
    // Function to check whether only the MSB and LSB are
    // set.
    public static boolean areSet(int n)
    {
        // Special case: 1 has only one bit, which is both
        // MSB and LSB.
        if (n == 1)
            return true;

        ArrayList<Integer> bits = new ArrayList<>();

        // Store the binary representation of the number.
        while (n > 0) {
            bits.add(n % 2);
            n /= 2;
        }

        int cnt = 0;

        // Count the number of set bits.
        for (int bit : bits)
            cnt += bit;

        // Check whether only the first and last bits are
        // set.
        return (cnt == 2 && bits.get(0) == 1
                && bits.get(bits.size() - 1) == 1);
    }

    public static void main(String[] args)
    {
        int n = 9;

        if (areSet(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
def areSet(n):
    # Special case: 1 has only one bit, which is both MSB and LSB.
    if n == 1:
        return True

    bits = []

    # Store the binary representation of the number.
    while n > 0:
        bits.append(n % 2)
        n //= 2

    cnt = 0

    # Count the number of set bits.
    for bit in bits:
        cnt += bit

    # Check whether only the first and last bits are set.
    return (cnt == 2 and bits[0] == 1 and bits[-1] == 1)


if __name__ == '__main__':
    n = 9

    if areSet(n):
        print('true')
    else:
        print('false')
C#
using System;
using System.Collections.Generic;

public class GFG {
    // Function to check whether only the MSB and LSB are
    // set.
    public static bool areSet(int n)
    {
        // Special case: 1 has only one bit, which is both
        // MSB and LSB.
        if (n == 1)
            return true;

        List<int> bits = new List<int>();

        // Store the binary representation of the number.
        while (n > 0) {
            bits.Add(n % 2);
            n /= 2;
        }

        int cnt = 0;

        // Count the number of set bits.
        foreach(int bit in bits) cnt += bit;

        // Check whether only the first and last bits are
        // set.
        return (cnt == 2 && bits[0] == 1
                && bits[bits.Count - 1] == 1);
    }

    public static void Main()
    {
        int n = 9;

        if (areSet(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
function areSet(n)
{
    // Special case: 1 has only one bit, which is both MSB
    // and LSB.
    if (n === 1)
        return true;

    let bits = [];

    // Store the binary representation of the number.
    while (n > 0) {
        bits.push(n % 2);
        n = Math.floor(n / 2);
    }

    let cnt = 0;

    // Count the number of set bits.
    for (let bit of bits)
        cnt += bit;

    // Check whether only the first and last bits are set.
    return (cnt === 2 && bits[0] === 1
            && bits[bits.length - 1] === 1);
}

// Driver Code
let n = 9;

if (areSet(n))
    console.log("true");
else
    console.log("false");

Output
true

[Expected Approach] Using the Property of Powers of Two - O(1) Time and O(1) Space

The idea is to use the observation that a number with only the MSB and LSB set is always of the form 2ᵏ + 1. Hence, subtracting 1 from such a number gives a power of two. So, after handling the edge cases n = 1 and n = 2, simply check whether n - 1 is a power of two using the bitwise expression x & (x - 1).

Let us understand with an example:
Input: n = 9

  • Consider n = 9, whose binary representation is 1001. Since n is neither 1 nor 2, check whether n - 1 = 8 is a power of two.
  • Call powerOfTwo(8). Here, 8 in binary is 1000.
  • Compute 8 & (8 - 1) = 1000 & 0111 = 0000, so 8 is a power of two.
  • Therefore, powerOfTwo(8) returns true, and areSet(9) also returns true.
  • Hence, the output is true, indicating that only the MSB and LSB are set in the binary representation of 9.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to check if a number is a power of two.
bool powerOfTwo(int n)
{
    return (!(n & n - 1));
}

// Function to check if only the first and last bits of a number are set.
bool areSet(int n)
{
    if (n == 1)
        return true;
    if (n == 2)
        return false;
    if (powerOfTwo(n - 1))
        return true;
    return 0;
}

int main()
{
    int n = 9;

    if (areSet(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.util.*;

public class GFG {

    // Function to check if a number is a power of two.
    public static boolean powerOfTwo(int n)
    {
        return n > 0 && (n & (n - 1)) == 0;
    }

    // Function to check if only the MSB and LSB are set.
    public static boolean areSet(int n)
    {
        if (n == 1)
            return true;
        if (n == 2)
            return false;

        return powerOfTwo(n - 1);
    }

    public static void main(String[] args)
    {
        int n = 9;

        if (areSet(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
def powerOfTwo(n):
    # Function to check if a number is a power of two.
    return (n & (n - 1)) == 0


def areSet(n):
    # Function to check if only the first and last bits of a number are set.
    if n == 1:
        return True
    if n == 2:
        return False
    if powerOfTwo(n - 1):
        return True
    return False


if __name__ == '__main__':
    n = 9

    if areSet(n):
        print('true')
    else:
        print('false')
C#
using System;

public class GFG {
    // Function to check if a number is a power of two.
    public static bool powerOfTwo(int n)
    {
        return (n & (n - 1)) == 0;
    }

    // Function to check if only the first and last bits of
    // a number are set.
    public static bool areSet(int n)
    {
        if (n == 1)
            return true;
        if (n == 2)
            return false;
        if (powerOfTwo(n - 1))
            return true;
        return false;
    }

    public static void Main()
    {
        int n = 9;

        if (areSet(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
function powerOfTwo(n)
{
    // Function to check if a number is a power of two.
    return (!(n & (n - 1)));
}

function areSet(n)
{
    // Function to check if only the first and last bits of
    // a number are set.
    if (n === 1)
        return true;
    if (n === 2)
        return false;
    if (powerOfTwo(n - 1))
        return true;
    return false;
}

// Driver Code
let n = 9;

if (areSet(n))
    console.log("true");
else
    console.log("false");

Output
true
Comment