Find most significant set bit of a number

Given a number, find the most significant bit number which is set bit and which is in power of two

Examples:

Input : 10
Output : 8
Binary representation of 10 is 1010
The most significant bit corresponds
to decimal number 8.

Input : 18
Output : 16



A simple solution is to one by one divide n by 2 until it becomes 0 and increment a count while doing this. This count actually represents the position of MSB.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple CPP program to find MSB number
// for given n.
#include <iostream>
using namespace std;
  
int setBitNumber(int n)
{
    if (n == 0)
        return 0;
  
    int msb = 0;
    while (n != 0) {
        n = n / 2;
        msb++;
    }
  
    return (1 << msb);
}
  
// Driver code
int main()
{
    int n = 0;
    cout << setBitNumber(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Java rogram to find 
// MSB number for given n.
import java.io.*;
  
class GFG 
{
static int setBitNumber(int n)
{
    if (n == 0)
        return 0;
  
    int msb = 0;
    while (n != 0)
    {
        n = n / 2;
        msb++;
    }
  
    return (1 << msb);
}
  
// Driver code
public static void main (String[] args) 
{
    int n = 0;
    System.out.println(setBitNumber(n));
}
}
  
// This code is contributed by ajit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Simple Python3 program 
# to find MSB number
# for given n.
def setBitNumber(n):
    if (n == 0):
        return 0;
  
    msb = 0;
    while (n > 0):
        n = int(n / 2);
        msb += 1;
  
    return (1 << msb);
  
# Driver code
n = 0;
print(setBitNumber(n));
      
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# rogram to find 
// MSB number for given n.
using System;
  
class GFG
{
static int setBitNumber(int n)
{
    if (n == 0)
        return 0;
  
    int msb = 0;
    while (n != 0)
    {
        n = n / 2;
        msb++;
    }
  
    return (1 << msb);
}
  
// Driver code
static public void Main ()
{
    int n = 0;
    Console.WriteLine(setBitNumber(n));
}
}
  
// This code is contributed 
// by akt_mit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Simple PhP program 
// to find MSB number
// for given n.
  
function setBitNumber($n)
{
    if ($n == 0)
        return 0;
  
    $msb = 0;
    while ($n != 0) 
    {
        $n = $n / 2;
        $msb++;
    }
  
    return (1 << $msb);
}
  
// Driver code
$n = 0;
echo setBitNumber($n);
      
// This code is contributed 
// by akt_mit
?>

chevron_right


Output:

0

An efficient solution for a fixed size integer (say 32 bits) is to one by one set bits, then add 1 so that only the bit after MSB is set. Finally right shift by 1 and return the answer. This solution does not require any condition checking.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find MSB number for given n.
#include <iostream>
using namespace std;
  
int setBitNumber(int n)
{
    // Below steps set bits after
    // MSB (including MSB)
  
    // Suppose n is 273 (binary
    // is 100010001). It does following
    // 100010001 | 010001000 = 110011001
    n |= n >> 1;
  
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set. It does following
    // 110011001 | 001100110 = 111111111
    n |= n >> 2;
  
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
  
    // Increment n by 1 so that
    // there is only one set bit
    // which is just before original
    // MSB. n now becomes 1000000000
    n = n + 1;
  
    // Return original MSB after shifting.
    // n now becomes 100000000
    return (n >> 1);
}
  
// Driver code
int main()
{
    int n = 273;
    cout << setBitNumber(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find MSB
// number for given n.
  
class GFG {
  
    static int setBitNumber(int n)
    {
  
        // Below steps set bits after
        // MSB (including MSB)
  
        // Suppose n is 273 (binary
        // is 100010001). It does following
        // 100010001 | 010001000 = 110011001
        n |= n >> 1;
  
        // This makes sure 4 bits
        // (From MSB and including MSB)
        // are set. It does following
        // 110011001 | 001100110 = 111111111
        n |= n >> 2;
  
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
  
        // Increment n by 1 so that
        // there is only one set bit
        // which is just before original
        // MSB. n now becomes 1000000000
        n = n + 1;
  
        // Return original MSB after shifting.
        // n now becomes 100000000
        return (n >> 1);
    }
  
    // Driver code
    public static void main(String arg[])
    {
        int n = 273;
        System.out.print(setBitNumber(n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find
# MSB number for given n.
  
def setBitNumber(n):
  
    # Below steps set bits after
    # MSB (including MSB)
   
    # Suppose n is 273 (binary 
    # is 100010001). It does following
    # 100010001 | 010001000 = 110011001
    n |= n>>1
   
    # This makes sure 4 bits
    # (From MSB and including MSB)
    # are set. It does following
    # 110011001 | 001100110 = 111111111
    n |= n>>2   
   
    n |= n>>4  
    n |= n>>8
    n |= n>>16
       
    # Increment n by 1 so that
    # there is only one set bit
    # which is just before original
    # MSB. n now becomes 1000000000
    n = n + 1
   
    # Return original MSB after shifting.
    # n now becomes 100000000
    return (n >> 1)
  
# Driver code
  
n = 273            
print(setBitNumber(n))
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find MSB number for given n.
using System;
  
class GFG {
  
    static int setBitNumber(int n)
    {
  
        // Below steps set bits after
        // MSB (including MSB)
  
        // Suppose n is 273 (binary
        // is 100010001). It does following
        // 100010001 | 010001000 = 110011001
        n |= n >> 1;
  
        // This makes sure 4 bits
        // (From MSB and including MSB)
        // are set. It does following
        // 110011001 | 001100110 = 111111111
        n |= n >> 2;
  
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
  
        // Increment n by 1 so that
        // there is only one set bit
        // which is just before original
        // MSB. n now becomes 1000000000
        n = n + 1;
  
        // Return original MSB after shifting.
        // n now becomes 100000000
        return (n >> 1);
    }
  
    // Driver code
    public static void Main()
    {
        int n = 273;
        Console.WriteLine(setBitNumber(n));
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// MSB number for given n.
  
function setBitNumber($n)
{
    // Below steps set bits 
    // after MSB (including MSB)
  
    // Suppose n is 273 (binary
    // is 100010001). It does
    // following 100010001 |
    // 010001000 = 110011001
    $n |= $n >> 1;
  
    // This makes sure 4 bits
    // (From MSB and including 
    // MSB) are set. It does 
    // following 110011001 | 
    // 001100110 = 111111111
    $n |= $n >> 2;
  
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
  
    // Increment n by 1 so 
    // that there is only 
    // one set bit which is
    // just before original
    // MSB. n now becomes 
    // 1000000000
    $n = $n + 1;
  
    // Return original MSB 
    // after shifting. n 
    // now becomes 100000000
    return ($n >> 1);
}
  
// Driver code
$n = 273;
echo setBitNumber($n);
  
// This code is contributed
// by akt_mit
?>

chevron_right


Output:

256

Time complexity is O(1).

Another Approach: Given a number n. First, find the position of the most significant set bit and then compute the value of the number with a set bit at k-th position.
Thanks Rohit Narayan for suggesting this method.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find MSB
// number for given n.
#include <bits/stdc++.h>
using namespace std;
  
int setBitNumber(int n)
{
  
    // To find the position
    // of the most significant
    // set bit
    int k = (int)(log2(n));
  
    // To return the the value
    // of the number with set
    // bit at k-th position
    return (int)(pow(2, k));
}
  
// Driver code
int main()
{
    int n = 273;
    cout << setBitNumber(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find MSB
// number for given n.
  
class GFG {
  
    static int setBitNumber(int n)
    {
  
        // To find the position of the
        // most significant set bit
        int k = (int)(Math.log(n) / Math.log(2));
  
        // To return the the value of the number
        // with set bit at k-th position
        return (int)(Math.pow(2, k));
    }
  
    // Driver code
    public static void main(String arg[])
    {
        int n = 273;
        System.out.print(setBitNumber(n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find
# MSB number for given n.
import math
  
def setBitNumber(n):
      
    # To find the position of
    # the most significant 
    # set bit
    k = int(math.log(n, 2))
      
    # To return the value 
    # of the number with set 
    # bit at k-th position
    return 2**k
  
# Driver code
n = 273        
print(setBitNumber(n))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find MSB
// number for given n.
using System;
  
public class GFG {
  
    static int setBitNumber(int n)
    {
  
        // To find the position of the
        // most significant set bit
        int k = (int)(Math.Log(n) / Math.Log(2));
  
        // To return the the value of the number
        // with set bit at k-th position
        return (int)(Math.Pow(2, k));
    }
  
    // Driver code
    static public void Main()
    {
        int n = 273;
        Console.WriteLine(setBitNumber(n));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find MSB
// number for given n.
  
function setBitNumber($n)
{
  
    // To find the position
    // of the most significant
    // set bit
    $k =(int)(log($n,2));
  
    // To return the the value
    // of the number with set
    // bit at k-th position
    return (int)(pow(2, $k));
}
  
// Driver code
    $n = 273;
    echo setBitNumber($n);
  
// This code is contributed
// by jit_t.
?>

chevron_right


Output:

256

This article is contributed by Devanshu Agarwal. 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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


2


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