Find most significant set bit of a number

Here given code implementation process.

// C Program 
// Find most significant set bit of a number
#include <stdio.h>

#include <math.h>

// Find position of most significant bit
void mostSignificantSetBit(int n)
{
	if (n <= 0)
	{
		return;
	}
	// Calculate total bits
	int bit = (int)(log(n) / log(2));
	// Get value of bit position
	int result = (int) pow(2, bit);
	// Display calculated result
	printf(" MostSignificant Set Bit of number %d is : %d \n", n, result);
}
int main()
{
	// Test Cases
	// 320 = (101000000)
	mostSignificantSetBit(320);
	// (1000) = (1111101000)
	mostSignificantSetBit(1000);
	// (54) = (110110) 
	mostSignificantSetBit(54);
	// 5 = (1001) 
	mostSignificantSetBit(5);
	return 0;
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
/*
  Java Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	public void mostSignificantSetBit(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		int bit = (int)(Math.log(n) / Math.log(2));
		// Get value of bit position
		int result = (int) Math.pow(2, bit);
		// Display calculated result
		System.out.print(" MostSignificant Set Bit of number " + n + " is : " + result + " \n");
	}
	public static void main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Cases
		// 320 = (101000000)
		task.mostSignificantSetBit(320);
		// (1000) = (1111101000)
		task.mostSignificantSetBit(1000);
		// (54) = (110110) 
		task.mostSignificantSetBit(54);
		// 5 = (1001) 
		task.mostSignificantSetBit(5);
	}
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
// Include header file
#include <iostream>
#include <math.h>
using namespace std;

/*
  C++ Program for
  Find most significant set bit of a number
*/

class BitPosition
{
	public:
		// Find position of most significant bit
		void mostSignificantSetBit(int n)
		{
			if (n <= 0)
			{
				return;
			}
			// Calculate total bits
			int bit = (int)(log(n) / log(2));
			// Get value of bit position
			int result = (int) pow(2, bit);
			// Display calculated result
			cout << " MostSignificant Set Bit of number " << n << " is : " << result << " \n";
		}
};
int main()
{
	BitPosition task = BitPosition();
	// Test Cases
	// 320 = (101000000)
	task.mostSignificantSetBit(320);
	// (1000) = (1111101000)
	task.mostSignificantSetBit(1000);
	// (54) = (110110)
	task.mostSignificantSetBit(54);
	// 5 = (1001)
	task.mostSignificantSetBit(5);
	return 0;
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
// Include namespace system
using System;
/*
  C# Program for
  Find most significant set bit of a number
*/
public class BitPosition
{
	// Find position of most significant bit
	public void mostSignificantSetBit(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		int bit = (int)(Math.Log(n) / Math.Log(2));
		// Get value of bit position
		int result = (int) Math.Pow(2, bit);
		// Display calculated result
		Console.Write(" MostSignificant Set Bit of number " + n + " is : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		BitPosition task = new BitPosition();
		// Test Cases
		// 320 = (101000000)
		task.mostSignificantSetBit(320);
		// (1000) = (1111101000)
		task.mostSignificantSetBit(1000);
		// (54) = (110110)
		task.mostSignificantSetBit(54);
		// 5 = (1001)
		task.mostSignificantSetBit(5);
	}
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
<?php
/*
  Php Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	public	function mostSignificantSetBit($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Calculate total bits
		$bit = (int)(intval(log($n) / log(2)));
		// Get value of bit position
		$result = (int) pow(2,$bit);
		// Display calculated result
		echo " MostSignificant Set Bit of number ". $n ." is : ". $result ." \n";
	}
}

function main()
{
	$task = new BitPosition();
	// Test Cases
	// 320 = (101000000)
	$task->mostSignificantSetBit(320);
	// (1000) = (1111101000)
	$task->mostSignificantSetBit(1000);
	// (54) = (110110)
	$task->mostSignificantSetBit(54);
	// 5 = (1001)
	$task->mostSignificantSetBit(5);
}
main();

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
/*
  Node Js Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	mostSignificantSetBit(n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		var bit = parseInt(((Math.log(n) / Math.log(2))));
		// Get value of bit position
		var result = parseInt(Math.pow(2, bit));
		// Display calculated result
		process.stdout.write(" MostSignificant Set Bit of number " + n + " is : " + result + " \n");
	}
}

function main()
{
	var task = new BitPosition();
	// Test Cases
	// 320 = (101000000)
	task.mostSignificantSetBit(320);
	// (1000) = (1111101000)
	task.mostSignificantSetBit(1000);
	// (54) = (110110)
	task.mostSignificantSetBit(54);
	// 5 = (1001)
	task.mostSignificantSetBit(5);
}
main();

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
import math
#   Python 3 Program for
#   Find most significant set bit of a number

class BitPosition :
	#  Find position of most significant bit
	def mostSignificantSetBit(self, n) :
		if (n <= 0) :
			return
		
		#  Calculate total bits
		bit = (int(math.log(n) / math.log(2)))
		#  Get value of bit position
		result = int(math.pow(2, bit))
		#  Display calculated result
		print(" MostSignificant Set Bit of number ", n ," is : ", result ," ")
	

def main() :
	task = BitPosition()
	#  Test Cases
	#  320 = (101000000)
	task.mostSignificantSetBit(320)
	#  (1000) = (1111101000)
	task.mostSignificantSetBit(1000)
	#  (54) = (110110) 
	task.mostSignificantSetBit(54)
	#  5 = (1001) 
	task.mostSignificantSetBit(5)

if __name__ == "__main__": main()

Output

 MostSignificant Set Bit of number  320  is :  256
 MostSignificant Set Bit of number  1000  is :  512
 MostSignificant Set Bit of number  54  is :  32
 MostSignificant Set Bit of number  5  is :  4
#   Ruby Program for
#   Find most significant set bit of a number

class BitPosition 
	#  Find position of most significant bit
	def mostSignificantSetBit(n) 
		if (n <= 0) 
			return
		end

		#  Calculate total bits
		bit = (Math.log(n) / Math.log(2)).to_i
		#  Get value of bit position
		result = (2**bit)
		#  Display calculated result
		print(" MostSignificant Set Bit of number ", n ," is : ", result ," \n")
	end

end

def main() 
	task = BitPosition.new()
	#  Test Cases
	#  320 = (101000000)
	task.mostSignificantSetBit(320)
	#  (1000) = (1111101000)
	task.mostSignificantSetBit(1000)
	#  (54) = (110110) 
	task.mostSignificantSetBit(54)
	#  5 = (1001) 
	task.mostSignificantSetBit(5)
end

main()

Output

 MostSignificant Set Bit of number 320 is : 256 
 MostSignificant Set Bit of number 1000 is : 512 
 MostSignificant Set Bit of number 54 is : 32 
 MostSignificant Set Bit of number 5 is : 4 
/*
  Scala Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	def mostSignificantSetBit(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		var bit: Int = ((Math.log(n) / Math.log(2)).toInt);
		// Get value of bit position
		var result: Int = (Math.pow(2, bit)).toInt;
		// Display calculated result
		print(" MostSignificant Set Bit of number " + n + " is : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitPosition = new BitPosition();
		// Test Cases
		// 320 = (101000000)
		task.mostSignificantSetBit(320);
		// (1000) = (1111101000)
		task.mostSignificantSetBit(1000);
		// (54) = (110110)
		task.mostSignificantSetBit(54);
		// 5 = (1001)
		task.mostSignificantSetBit(5);
	}
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4
import Foundation
/*
  Swift 4 Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	func mostSignificantSetBit(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		let bit: Int = Int(log(Double(n)) / log(2.0));
		// Get value of bit position
		let result: Int = Int(pow(2.0, Double(bit)));
	// Display calculated result
	print(" MostSignificant Set Bit of number ", n ," is : ", result ," ");
}
}
func main()
{
	let task: BitPosition = BitPosition();
	// Test Cases
	// 320 = (101000000)
	task.mostSignificantSetBit(320);
	// (1000) = (1111101000)
	task.mostSignificantSetBit(1000);
	// (54) = (110110)
	task.mostSignificantSetBit(54);
	// 5 = (1001)
	task.mostSignificantSetBit(5);
}
main();

Output

 MostSignificant Set Bit of number  320  is :  256
 MostSignificant Set Bit of number  1000  is :  512
 MostSignificant Set Bit of number  54  is :  32
 MostSignificant Set Bit of number  5  is :  4
/*
  Kotlin Program for
  Find most significant set bit of a number
*/
class BitPosition
{
	// Find position of most significant bit
	fun mostSignificantSetBit(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate total bits
		var bit: Int = (Math.log(n.toDouble()) / Math.log(2.0)).toInt();
		// Get value of bit position
		var result: Int = (Math.pow(2.0, bit.toDouble())).toInt();
		// Display calculated result
		print(" MostSignificant Set Bit of number " + n + " is : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitPosition = BitPosition();
	// Test Cases
	// 320 = (101000000)
	task.mostSignificantSetBit(320);
	// (1000) = (1111101000)
	task.mostSignificantSetBit(1000);
	// (54) = (110110)
	task.mostSignificantSetBit(54);
	// 5 = (1001)
	task.mostSignificantSetBit(5);
}

Output

 MostSignificant Set Bit of number 320 is : 256
 MostSignificant Set Bit of number 1000 is : 512
 MostSignificant Set Bit of number 54 is : 32
 MostSignificant Set Bit of number 5 is : 4


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved