Activate all bits of a number
Here given code implementation process.
// C Program
// Activate all bits of a number
#include <stdio.h>
// Active all the bits of present in a number
void activeAllBits(int num)
{
int n = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
printf(" Number : %d", num);
printf("\n After Active : %d\n", n);
}
int main()
{
// (16) 10000 => 11111 (31)
activeAllBits(16);
// (99) 1100011 => 1111111 (127)
activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
activeAllBits(9999999);
return 0;
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
/*
Java Program for
Activate all bits of a number
*/
public class BitActivation
{
// Active all the bits of present in a number
public void activeAllBits(int num)
{
int n = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
System.out.print(" Number : " + num);
System.out.print("\n After Active : " + n + "\n");
}
public static void main(String[] args)
{
BitActivation task = new BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Activate all bits of a number
*/
class BitActivation
{
public:
// Active all the bits of present in a number
void activeAllBits(int num)
{
int n = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
cout << " Number : " << num;
cout << "\n After Active : " << n << "\n";
}
};
int main()
{
BitActivation task = BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
return 0;
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
// Include namespace system
using System;
/*
C# Program for
Activate all bits of a number
*/
public class BitActivation
{
// Active all the bits of present in a number
public void activeAllBits(int num)
{
int n = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
Console.Write(" Number : " + num);
Console.Write("\n After Active : " + n + "\n");
}
public static void Main(String[] args)
{
BitActivation task = new BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
<?php
/*
Php Program for
Activate all bits of a number
*/
class BitActivation
{
// Active all the bits of present in a number
public function activeAllBits($num)
{
$n = $num;
$n = $n | $n >> 1;
$n = $n | $n >> 2;
$n = $n | $n >> 4;
$n = $n | $n >> 8;
$n = $n | $n >> 16;
// Display calculated result
echo " Number : ". $num;
echo "\n After Active : ". $n ."\n";
}
}
function main()
{
$task = new BitActivation();
// (16) 10000 => 11111 (31)
$task->activeAllBits(16);
// (99) 1100011 => 1111111 (127)
$task->activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
$task->activeAllBits(9999999);
}
main();
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
/*
Node Js Program for
Activate all bits of a number
*/
class BitActivation
{
// Active all the bits of present in a number
activeAllBits(num)
{
var n = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
process.stdout.write(" Number : " + num);
process.stdout.write("\n After Active : " + n + "\n");
}
}
function main()
{
var task = new BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
main();
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
# Python 3 Program for
# Activate all bits of a number
class BitActivation :
# Active all the bits of present in a number
def activeAllBits(self, num) :
n = num
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
# Display calculated result
print(" Number : ", num, end = "")
print("\n After Active : ", n )
def main() :
task = BitActivation()
# (16) 10000 => 11111 (31)
task.activeAllBits(16)
# (99) 1100011 => 1111111 (127)
task.activeAllBits(99)
# (9999999) 100110001001011001111111 =>
# (16777215) 111111111111111111111111
task.activeAllBits(9999999)
if __name__ == "__main__": main()
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
# Ruby Program for
# Activate all bits of a number
class BitActivation
# Active all the bits of present in a number
def activeAllBits(num)
n = num
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
# Display calculated result
print(" Number : ", num)
print("\n After Active : ", n ,"\n")
end
end
def main()
task = BitActivation.new()
# (16) 10000 => 11111 (31)
task.activeAllBits(16)
# (99) 1100011 => 1111111 (127)
task.activeAllBits(99)
# (9999999) 100110001001011001111111 =>
# (16777215) 111111111111111111111111
task.activeAllBits(9999999)
end
main()
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
/*
Scala Program for
Activate all bits of a number
*/
class BitActivation
{
// Active all the bits of present in a number
def activeAllBits(num: Int): Unit = {
var n: Int = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
print(" Number : " + num);
print("\n After Active : " + n + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitActivation = new BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
/*
Swift 4 Program for
Activate all bits of a number
*/
class BitActivation
{
// Active all the bits of present in a number
func activeAllBits(_ num: Int)
{
var n: Int = num;
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Display calculated result
print(" Number : ", num, terminator: "");
print("\n After Active : ", n );
}
}
func main()
{
let task: BitActivation = BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
main();
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
/*
Kotlin Program for
Activate all bits of a number
*/
class BitActivation
{
// Active all the bits of present in a number
fun activeAllBits(num: Int): Unit
{
var n: Int = num;
n = n or (n shr 1);
n = n or (n shr 2);
n = n or (n shr 4);
n = n or (n shr 8);
n = n or (n shr 16);
// Display calculated result
print(" Number : " + num);
print("\n After Active : " + n + "\n");
}
}
fun main(args: Array <String> ): Unit
{
var task: BitActivation = BitActivation();
// (16) 10000 => 11111 (31)
task.activeAllBits(16);
// (99) 1100011 => 1111111 (127)
task.activeAllBits(99);
// (9999999) 100110001001011001111111 =>
// (16777215) 111111111111111111111111
task.activeAllBits(9999999);
}
Output
Number : 16
After Active : 31
Number : 99
After Active : 127
Number : 9999999
After Active : 16777215
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