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)
Similar Reads
Find most significant set bit of a number
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: 10Output: 8Explanation:Greatest number which is a Power of 2 less than 10 is 8Binary representation of 10 is 1010The most significant bit corresp
15 min read
Find most significant bit of a number X in base Y
Given two positive integers X and Y, the task is to find the MSB of X, in the given base Y.Examples: Input: X = 55, Y = 3 Output: 2 Explanation: 55 is 2001 in base 3 with first digit as 2.Input: X = 123, Y = 10 Output: 1 Explanation: 123 is 123 in base 10 with first digit 1. Approach: Let the task t
4 min read
Print 'K'th least significant bit of a number
A number N is given. We need to print its 'K'th Least Significant Bit.Examples : Input : num = 10, k = 4 Output : 1 Explanation : Binary Representation of 10 is 1010. 4th LSB is 1. Input : num = 16, k = 3 Output : 0 Explanation : Binary Representation of 16 is 10000. 3rd LSB is 0.Recommended Practic
3 min read
Unset least significant K bits of a given number
Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000
4 min read
Invert the Kth most significant bit of N
Given two non-negative integers N and K, the task is to invert the Kth most significant bit of N and print the number obtained after inverting the bit.Examples: Input: N = 10, K = 1 Output: 2 The binary representation of 10 is 1010. After inverting the first bit it becomes 0010 whose decimal equival
7 min read
Set the K-th bit of a given number
Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.Examples: Input : n = 10, k = 2 Output : 1
4 min read
Same Number Of Set Bits As N
Given a positive integer N, find out how many positive integers strictly less than N have the same number of set bits as N. Examples: Input : 8 Output :3 Explanation: Binary representation of 8 : 1000, so number of set bits in 8 is 1. So the integers less than 8 with same number of set bits are : 4,
9 min read
Round-off a number to a given number of significant digits
Given a positive number n (n > 1), round-off this number to a given no. of significant digits, d.Examples: Input : n = 139.59 d = 4 Output : The number after rounding-off is 139.6 . The number 139.59 has 5 significant figures and for rounding-off the number to 4 significant figures, 139.59 is con
8 min read
Find square root of a number using Bit Manipulation
Given a non-negative integer N, the task is to find the square root of N using bitwise operations. If the integer is not the perfect square, return largest integer that is smaller than or equal to square root of N i.e., floor(âN). Examples: Input: N = 36Output: 6Explanation: The square root of 36 is
7 min read
Find position of left most dis-similar bit for two numbers
Given two numbers n1 and n2. The task is to find the position of first mismatching bit in the binary representation of the two numbers from left. We need to find this bit after making lengths of binary representations of both numbers same. We make lengths same by appending 0s in the smaller number.
7 min read