Find nearest power of 2 less than or equal to a number

Here given code implementation process.

``````// C program
// Find nearest power of 2 less than or equal to a number
#include <stdio.h>

// Find nearest power of given number
void nearestPower2(int num)
{
int n = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
printf("\n Number :  %d", num);
printf("\n Power  :  %d\n", n);
}
int main(int argc, char
const *argv[])
{
// Test Cases
nearestPower2(10);
nearestPower2(31);
nearestPower2(64);
nearestPower2(43);
return 0;
}``````

Output

`````` Number :  10
Power  :  8

Number :  31
Power  :  16

Number :  64
Power  :  64

Number :  43
Power  :  32``````
``````/*
Java program
Find nearest power of 2 less than or equal to a number
*/
public class Power
{

// Find nearest power of given number
public void nearestPower2(int num)
{
int n = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
System.out.print("\n Number : " + num );
System.out.print("\n Power : " + n + "\n");
}

public static void main(String[] args)
{
// Test Cases
}
}``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Find nearest power of 2 less than or equal to a number
*/

class Power
{
public:
// Find nearest power of given number
void nearestPower2(int num)
{
int n = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
cout << "\n Number : " << num;
cout << "\n Power : " << n << "\n";
}
};
int main()
{
// Test Cases
return 0;
}``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````// Include namespace system
using System;
/*
C# program
Find nearest power of 2 less than or equal to a number
*/
public class Power
{
// Find nearest power of given number
public void nearestPower2(int num)
{
int n = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
Console.Write("\n Number : " + num);
Console.Write("\n Power : " + n + "\n");
}
public static void Main(String[] args)
{
// Test Cases
}
}``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````<?php
/*
Php program
Find nearest power of 2 less than or equal to a number
*/
class Power
{
// Find nearest power of given number
public	function nearestPower2(\$num)
{
\$n = \$num;
// First set all bits
\$n = \$n | \$n >> 1;
\$n = \$n | \$n >> 2;
\$n = \$n | \$n >> 4;
\$n = \$n | \$n >> 8;
\$n = \$n | \$n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
\$n = \$n ^ (\$n >> 1);
// Display calculated result
echo "\n Number : ". \$num;
echo "\n Power : ". \$n ."\n";
}
}

function main()
{
// Test Cases
}
main();``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````/*
Node Js program
Find nearest power of 2 less than or equal to a number
*/
class Power
{
// Find nearest power of given number
nearestPower2(num)
{
var n = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
process.stdout.write("\n Number : " + num);
process.stdout.write("\n Power : " + n + "\n");
}
}

function main()
{
// Test Cases
}
main();``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````#   Python 3 program
#   Find nearest power of 2 less than or equal to a number

class Power :
#  Find nearest power of given number
def nearestPower2(self, num) :
n = num
#  First set all bits
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
#  Get final result
#  n are indicates all active bits of given a number
#  So shift n one by left and perform xor operation
n = n ^ (n >> 1)
#  Display calculated result
print("\n Number : ", num, end = "")
print("\n Power : ", n )

def main() :
#  Test Cases

if __name__ == "__main__": main()``````

Output

`````` Number :  10
Power :  8

Number :  31
Power :  16

Number :  64
Power :  64

Number :  43
Power :  32``````
``````#   Ruby program
#   Find nearest power of 2 less than or equal to a number

class Power
#  Find nearest power of given number
def nearestPower2(num)
n = num
#  First set all bits
n = n | n >> 1
n = n | n >> 2
n = n | n >> 4
n = n | n >> 8
n = n | n >> 16
#  Get final result
#  n are indicates all active bits of given a number
#  So shift n one by left and perform xor operation
n = n ^ (n >> 1)
#  Display calculated result
print("\n Number : ", num)
print("\n Power : ", n ,"\n")
end

end

def main()
#  Test Cases
end

main()``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32
``````
``````/*
Scala program
Find nearest power of 2 less than or equal to a number
*/
class Power
{
// Find nearest power of given number
def nearestPower2(num: Int): Unit = {
var n: Int = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
print("\n Number : " + num);
print("\n Power : " + n + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Power = new Power();
// Test Cases
}
}``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````
``````/*
Swift 4 program
Find nearest power of 2 less than or equal to a number
*/
class Power
{
// Find nearest power of given number
func nearestPower2(_ num: Int)
{
var n: Int = num;
// First set all bits
n = n | n >> 1;
n = n | n >> 2;
n = n | n >> 4;
n = n | n >> 8;
n = n | n >> 16;
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n ^ (n >> 1);
// Display calculated result
print("\n Number : ", num, terminator: "");
print("\n Power : ", n );
}
}
func main()
{
// Test Cases
}
main();``````

Output

`````` Number :  10
Power :  8

Number :  31
Power :  16

Number :  64
Power :  64

Number :  43
Power :  32``````
``````/*
Kotlin program
Find nearest power of 2 less than or equal to a number
*/
class Power
{
// Find nearest power of given number
fun nearestPower2(num: Int): Unit
{
var n: Int = num;
// First set all bits
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);
// Get final result
// n are indicates all active bits of given a number
// So shift n one by left and perform xor operation
n = n xor (n shr 1);
// Display calculated result
print("\n Number : " + num);
print("\n Power : " + n + "\n");
}
}
fun main(args: Array <String> ): Unit
{
// Test Cases
}``````

Output

`````` Number : 10
Power : 8

Number : 31
Power : 16

Number : 64
Power : 64

Number : 43
Power : 32``````

Comment

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.