Remove the most significant set bit of a number
Here given code implementation process.
// C Program
// Remove the most significant set bit of a number
#include <stdio.h>
// Remove a most significant bit of given number
void removeSignificantBit(int num)
{
if (num <= 0)
{
return;
}
int r = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
int value = r & num;
// Display calculated result
printf(" Number : %d \n", num);
printf(" Result : %d \n", value);
}
int main()
{
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
removeSignificantBit(5);
return 0;
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
/*
Java Program
Remove the most significant set bit of a number
*/
public class SignificantBit
{
// Remove a most significant bit of given number
public void removeSignificantBit(int num)
{
if (num <= 0)
{
return;
}
int r = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
int value = r & num;
// Display calculated result
System.out.print(" Number : " + num + " \n");
System.out.print(" Result : " + value + " \n");
}
public static void main(String[] args)
{
SignificantBit task = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Remove the most significant set bit of a number
*/
class SignificantBit
{
public:
// Remove a most significant bit of given number
void removeSignificantBit(int num)
{
if (num <= 0)
{
return;
}
int r = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
int value = r #
// Display calculated result
cout << " Number : " << num << " \n";
cout << " Result : " << value << " \n";
}
};
int main()
{
SignificantBit *task = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task->removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task->removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task->removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task->removeSignificantBit(5);
return 0;
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
// Include namespace system
using System;
/*
Csharp Program
Remove the most significant set bit of a number
*/
public class SignificantBit
{
// Remove a most significant bit of given number
public void removeSignificantBit(int num)
{
if (num <= 0)
{
return;
}
int r = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
int value = r & num;
// Display calculated result
Console.Write(" Number : " + num + " \n");
Console.Write(" Result : " + value + " \n");
}
public static void Main(String[] args)
{
SignificantBit task = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
package main
import "fmt"
/*
Go Program
Remove the most significant set bit of a number
*/
type SignificantBit struct {}
func getSignificantBit() * SignificantBit {
var me *SignificantBit = &SignificantBit {}
return me
}
// Remove a most significant bit of given number
func(this SignificantBit) removeSignificantBit(num int) {
if num <= 0 {
return
}
var r int = num >> 1
r = r | (r >> 1)
r = r | (r >> 2)
r = r | (r >> 4)
r = r | (r >> 8)
r = r | (r >> 16)
// Remove most significant bit
var value int = r & num
// Display calculated result
fmt.Print(" Number : ", num, " \n")
fmt.Print(" Result : ", value, " \n")
}
func main() {
var task * SignificantBit = getSignificantBit()
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320)
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000)
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54)
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5)
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
<?php
/*
Php Program
Remove the most significant set bit of a number
*/
class SignificantBit
{
// Remove a most significant bit of given number
public function removeSignificantBit($num)
{
if ($num <= 0)
{
return;
}
$r = $num >> 1;
$r = $r | ($r >> 1);
$r = $r | ($r >> 2);
$r = $r | ($r >> 4);
$r = $r | ($r >> 8);
$r = $r | ($r >> 16);
// Remove most significant bit
$value = $r & $num;
// Display calculated result
echo(" Number : ".$num.
" \n");
echo(" Result : ".$value.
" \n");
}
}
function main()
{
$task = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
$task->removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
$task->removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
$task->removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
$task->removeSignificantBit(5);
}
main();
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
/*
Node JS Program
Remove the most significant set bit of a number
*/
class SignificantBit
{
// Remove a most significant bit of given number
removeSignificantBit(num)
{
if (num <= 0)
{
return;
}
var r = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
var value = r & num;
// Display calculated result
process.stdout.write(" Number : " + num + " \n");
process.stdout.write(" Result : " + value + " \n");
}
}
function main()
{
var task = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
main();
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
# Python 3 Program
# Remove the most significant set bit of a number
class SignificantBit :
# Remove a most significant bit of given number
def removeSignificantBit(self, num) :
if (num <= 0) :
return
r = num >> 1
r = r | (r >> 1)
r = r | (r >> 2)
r = r | (r >> 4)
r = r | (r >> 8)
r = r | (r >> 16)
# Remove most significant bit
value = r & num
# Display calculated result
print(" Number : ", num ," ")
print(" Result : ", value ," ")
def main() :
task = SignificantBit()
# Test A
# 320 = (101000000)
# Remove ↑
# ---------------------
# 1000000)
# Result : 64
task.removeSignificantBit(320)
# Test B
# (1000) = (1111101000)
# Remove ↑
# 111101000
# ---------------------
# Result : 488
task.removeSignificantBit(1000)
# Test C
# (54) = (110110)
# Remove ↑
# 10110
# ---------------------
# Result : 22
task.removeSignificantBit(54)
# Test D
# 5 = (101)
# Remove ↑
# 01
# ---------------------
# Result : 1
task.removeSignificantBit(5)
if __name__ == "__main__": main()
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
# Ruby Program
# Remove the most significant set bit of a number
class SignificantBit
# Remove a most significant bit of given number
def removeSignificantBit(num)
if (num <= 0)
return
end
r = num >> 1
r = r | (r >> 1)
r = r | (r >> 2)
r = r | (r >> 4)
r = r | (r >> 8)
r = r | (r >> 16)
# Remove most significant bit
value = r & num
# Display calculated result
print(" Number : ", num ," \n")
print(" Result : ", value ," \n")
end
end
def main()
task = SignificantBit.new()
# Test A
# 320 = (101000000)
# Remove ↑
# ---------------------
# 1000000)
# Result : 64
task.removeSignificantBit(320)
# Test B
# (1000) = (1111101000)
# Remove ↑
# 111101000
# ---------------------
# Result : 488
task.removeSignificantBit(1000)
# Test C
# (54) = (110110)
# Remove ↑
# 10110
# ---------------------
# Result : 22
task.removeSignificantBit(54)
# Test D
# 5 = (101)
# Remove ↑
# 01
# ---------------------
# Result : 1
task.removeSignificantBit(5)
end
main()
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
/*
Scala Program
Remove the most significant set bit of a number
*/
class SignificantBit()
{
// Remove a most significant bit of given number
def removeSignificantBit(num: Int): Unit = {
if (num <= 0)
{
return;
}
var r: Int = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
var value: Int = r & num;
// Display calculated result
print(" Number : " + num + " \n");
print(" Result : " + value + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SignificantBit = new SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
/*
Swift 4 Program
Remove the most significant set bit of a number
*/
class SignificantBit
{
// Remove a most significant bit of given number
func removeSignificantBit(_ num: Int)
{
if (num <= 0)
{
return;
}
var r: Int = num >> 1;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);
r = r | (r >> 8);
r = r | (r >> 16);
// Remove most significant bit
let value: Int = r & num;
// Display calculated result
print(" Number : ", num );
print(" Result : ", value );
}
}
func main()
{
let task: SignificantBit = SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
main();
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
/*
Kotlin Program
Remove the most significant set bit of a number
*/
class SignificantBit
{
// Remove a most significant bit of given number
fun removeSignificantBit(num: Int): Unit
{
if (num <= 0)
{
return;
}
var r: Int = num shr 1;
r = r or(r shr 1);
r = r or(r shr 2);
r = r or(r shr 4);
r = r or(r shr 8);
r = r or(r shr 16);
// Remove most significant bit
val value: Int = r and num;
// Display calculated result
print(" Number : " + num + " \n");
print(" Result : " + value + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: SignificantBit = SignificantBit();
// Test A
// 320 = (101000000)
// Remove ↑
// ---------------------
// 1000000)
// Result : 64
task.removeSignificantBit(320);
// Test B
// (1000) = (1111101000)
// Remove ↑
// 111101000
// ---------------------
// Result : 488
task.removeSignificantBit(1000);
// Test C
// (54) = (110110)
// Remove ↑
// 10110
// ---------------------
// Result : 22
task.removeSignificantBit(54);
// Test D
// 5 = (101)
// Remove ↑
// 01
// ---------------------
// Result : 1
task.removeSignificantBit(5);
}
Output
Number : 320
Result : 64
Number : 1000
Result : 488
Number : 54
Result : 22
Number : 5
Result : 1
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