Find most significant set bit of a number
Last Updated :
06 Apr, 2023
Given a number, find the greatest number less than the given a number which is the power of two or find the most significant bit number .
Examples:
Input: 10
Output: 8
Explanation:
Greatest number which is a Power of 2 less than 10 is 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++
#include <iostream>
using namespace std;
int setBitNumber( int n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (1 << msb);
}
int main()
{
int n = 0;
cout << setBitNumber(n);
n = ~( int )0;
cout << "\n" << (unsigned int )setBitNumber(n);
return 0;
}
|
C
#include <stdio.h>
int setBitNumber( int n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (1 << msb);
}
int main() {
int n = 0;
printf ( "%d \n" ,setBitNumber(n));
n = ~( int )0;
int ns = (unsigned int )setBitNumber(n);
printf ( "%d" ,ns);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int setBitNumber( int n)
{
if (n == 0 )
return 0 ;
int msb = 0 ;
n = n / 2 ;
while (n != 0 ) {
n = n / 2 ;
msb++;
}
return ( 1 << msb);
}
public static void main(String[] args)
{
int n = 0 ;
System.out.println(setBitNumber(n));
}
}
|
Python3
def setBitNumber(n):
if (n = = 0 ):
return 0 ;
msb = 0 ;
n = int (n / 2 );
while (n > 0 ):
n = int (n / 2 );
msb + = 1 ;
return ( 1 << msb);
n = 0 ;
print (setBitNumber(n));
|
C#
using System;
class GFG {
static int setBitNumber( int n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (1 << msb);
}
static public void Main()
{
int n = 0;
Console.WriteLine(setBitNumber(n));
}
}
|
PHP
<?php
function setBitNumber( $n )
{
if ( $n == 0)
return 0;
$msb = 0;
$n = $n / 2;
while ( $n != 0)
{
$n = $n / 2;
$msb ++;
}
return (1 << $msb );
}
$n = 0;
echo setBitNumber( $n );
?>
|
Javascript
<script>
function setBitNumber(n)
{
if (n == 0)
return 0;
let msb = 0;
n = n / 2;
while (n != 0)
{
n = $n / 2;
msb++;
}
return (1 << msb);
}
let n = 0;
document.write (setBitNumber(n));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
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++
#include <iostream>
#include <limits.h>
using namespace std;
int setBitNumber( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = ((n + 1) >> 1) |
(n & (1 << (( sizeof (n) * CHAR_BIT)-1)));
return n;
}
int main()
{
int n = 273;
cout << setBitNumber(n);
n = ~( int )0;
cout << "\n" << (unsigned int )setBitNumber(n);
return 0;
}
|
C
#include <stdio.h>
#include <limits.h>
int setBitNumber( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = ((n + 1) >> 1) |
(n & (1 << (( sizeof (n) * CHAR_BIT)-1)));
return n;
}
int main() {
int n = 273;
printf ( "%d\n" ,setBitNumber(n));
return 0;
}
|
Java
class GFG {
static int setBitNumber( int n)
{
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n = n + 1 ;
return (n >> 1 );
}
public static void main(String arg[])
{
int n = 273 ;
System.out.print(setBitNumber(n));
}
}
|
Python3
def setBitNumber(n):
n | = n>> 1
n | = n>> 2
n | = n>> 4
n | = n>> 8
n | = n>> 16
n = n + 1
return (n >> 1 )
n = 273
print (setBitNumber(n))
|
C#
using System;
class GFG {
static int setBitNumber( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
public static void Main()
{
int n = 273;
Console.WriteLine(setBitNumber(n));
}
}
|
PHP
<?php
function setBitNumber( $n )
{
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
$n = $n + 1;
return ( $n >> 1);
}
$n = 273;
echo setBitNumber( $n );
?>
|
Javascript
<script>
function setBitNumber(n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
let n = 273;
document.write(setBitNumber(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Using __builtin_clz(x) (GCC builtin function)
Say for a fixed integer (32 bits), count the number of leading zeroes by using the built-in function and subtract it from 31 to get the position of MSB from left, then return the MSB using left shift operation on 1.
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++
#include <bits/stdc++.h>
using namespace std;
int setBitNumber( int n)
{
int k = __builtin_clz(n);
return 1 << (31 - k);
}
int main()
{
int n = 273;
cout << setBitNumber(n);
n = ~( int )0;
cout << "\n" << (unsigned int )setBitNumber(n);
return 0;
}
|
Java
import java.lang.*;
public class Main {
public static int setBitNumber( int n) {
int k = Integer.numberOfLeadingZeros(n);
return 1 << ( 31 - k);
}
public static void main(String[] args) {
int n = 273 ;
System.out.println(setBitNumber(n));
n = ~( int ) 0 ;
System.out.println(( int )setBitNumber(n));
}
}
|
C#
using System;
public class MainClass {
public static int SetBitNumber( int n)
{
int k = 32 - Convert.ToString(n, 2).Length;
return 1 << (31 - k);
}
public static void Main()
{
int n = 273;
Console.WriteLine(SetBitNumber(n));
n = ~( int )0;
Console.WriteLine(( uint )SetBitNumber(n));
}
}
|
Javascript
function setBitNumber(n) {
let k = 31 - Math.floor(Math.log2(n));
return 1 << k;
}
let n = 273;
console.log(setBitNumber(n));
n = ~(0);
console.log(setBitNumber(n));
|
Time Complexity: O(1)
Auxiliary Space: 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++
#include <bits/stdc++.h>
using namespace std;
int setBitNumber( int n)
{
int ans = 1;
while (n) {
ans *= 2;
n /= 2;
}
ans/=2;
return ans;
}
int main()
{
int n = 273;
cout << setBitNumber(n);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
int setBitNumber( int n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (1 << msb);
}
int main() {
int n = 273;
printf ( "%d" ,setBitNumber(n));
return 0;
}
|
Java
class GFG {
static int setBitNumber( int n)
{
int k = ( int )(Math.log(n) / Math.log( 2 ));
return 1 << k;
}
public static void main(String arg[])
{
int n = 273 ;
System.out.print(setBitNumber(n));
}
}
|
Python3
import math
def setBitNumber(n):
k = int (math.log(n, 2 ))
return 1 << k
n = 273
print (setBitNumber(n))
|
C#
using System;
public class GFG {
static int setBitNumber( int n)
{
int k = ( int )(Math.Log(n) / Math.Log(2));
return 1 << k;
}
static public void Main()
{
int n = 273;
Console.WriteLine(setBitNumber(n));
}
}
|
PHP
<?php
function setBitNumber( $n )
{
$k =(int)(log( $n , 2));
return 1 << $k ;
}
$n = 273;
echo setBitNumber( $n );
?>
|
Javascript
<script>
function setBitNumber(n)
{
let k = parseInt(Math.log(n) / Math.log(2), 10);
return 1 << k;
}
let n = 273;
document.write(setBitNumber(n));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...