# Reverse the bits of a number

Here given code implementation process.

``````// C Program
// Reverse the bits of a number
#include <stdio.h>

// Reverse the bits element of a number
void reverseBits(int num)
{
int result = 0;
int n = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n /= 2;
}
// Display given number
printf(" Number : %d", num);
// After the reversing bits
printf("\n Output : %d\n\n", result);
}
int main()
{
// Test cases
// 10000 => 00001 (1)
reverseBits(16);
// 100011 => 110001 (49)
reverseBits(35);
// 10100  => 00101 (5)
reverseBits(20);
return 0;
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````/*
Java Program for
Reverse the bits of a number
*/
public class InvertBits
{
// Reverse the bits element of a number
public void reverseBits(int num)
{
int result = 0;
int n = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n /= 2;
}
// Display given number
System.out.print(" Number : " + num);
// After the reversing bits
System.out.print("\n Output : " + result + "\n\n");
}
public static void main(String[] args)
{
InvertBits task = new InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Reverse the bits of a number
*/
class InvertBits
{
public:
// Reverse the bits element of a number
void reverseBits(int num)
{
int result = 0;
int n = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n &1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n /= 2;
}
// Display given number
cout << " Number : " << num;
// After the reversing bits
cout << "\n Output : " << result << "\n\n";
}
};
int main()
{
InvertBits task = InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
return 0;
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````// Include namespace system
using System;
/*
C# Program for
Reverse the bits of a number
*/
public class InvertBits
{
// Reverse the bits element of a number
public void reverseBits(int num)
{
int result = 0;
int n = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n /= 2;
}
// Display given number
Console.Write(" Number : " + num);
// After the reversing bits
Console.Write("\n Output : " + result + "\n\n");
}
public static void Main(String[] args)
{
InvertBits task = new InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````<?php
/*
Php Program for
Reverse the bits of a number
*/
class InvertBits
{
// Reverse the bits element of a number
public	function reverseBits(\$num)
{
\$result = 0;
\$n = \$num;
// Execute loop until n is less than 1
while (\$n > 0)
{
if (\$result != 0)
{
\$result = \$result << 1;
}
if ((\$n & 1) == 1)
{
// When first bit is active
\$result = \$result ^ 1;
}
\$n = intval(\$n / 2);
}
// Display given number
echo " Number : ". \$num;
// After the reversing bits
echo "\n Output : ". \$result ."\n\n";
}
}

function main()
{
\$task = new InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
main();``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````/*
Node Js Program for
Reverse the bits of a number
*/
class InvertBits
{
// Reverse the bits element of a number
reverseBits(num)
{
var result = 0;
var n = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n = parseInt(n / 2);
}
// Display given number
process.stdout.write(" Number : " + num);
// After the reversing bits
process.stdout.write("\n Output : " + result + "\n\n");
}
}

function main()
{
var task = new InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
main();``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````#   Python 3 Program for
#   Reverse the bits of a number

class InvertBits :
#  Reverse the bits element of a number
def reverseBits(self, num) :
result = 0
n = num
#  Execute loop until n is less than 1
while (n > 0) :
if (result != 0) :
result = result << 1

if ((n & 1) == 1) :
#  When first bit is active
result = result ^ 1

n = int(n / 2)

#  Display given number
print(" Number : ", num, end = "")
#  After the reversing bits
print("\n Output : ", result ,"\n")

def main() :
#  Test cases
#  10000 => 00001 (1)
#  100011 => 110001 (49)
#  10100  => 00101 (5)

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

#### Output

`````` Number :  16
Output :  1

Number :  35
Output :  49

Number :  20
Output :  5
``````
``````#   Ruby Program for
#   Reverse the bits of a number

class InvertBits
#  Reverse the bits element of a number
def reverseBits(num)
result = 0
n = num
#  Execute loop until n is less than 1
while (n > 0)
if (result != 0)
result = result << 1
end

if ((n & 1) == 1)
#  When first bit is active
result = result ^ 1
end

n /= 2
end

#  Display given number
print(" Number : ", num)
#  After the reversing bits
print("\n Output : ", result ,"\n\n")
end

end

def main()
#  Test cases
#  10000 => 00001 (1)
#  100011 => 110001 (49)
#  10100  => 00101 (5)
end

main()``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5

``````
``````/*
Scala Program for
Reverse the bits of a number
*/
class InvertBits
{
// Reverse the bits element of a number
def reverseBits(num: Int): Unit = {
var result: Int = 0;
var n: Int = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n = (n / 2).toInt;
}
// Display given number
print(" Number : " + num);
// After the reversing bits
print("\n Output : " + result + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: InvertBits = new InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````
``````/*
Swift 4 Program for
Reverse the bits of a number
*/
class InvertBits
{
// Reverse the bits element of a number
func reverseBits(_ num: Int)
{
var result: Int = 0;
var n: Int = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result  != 0)
{
result = result << 1;
}
if ((n & 1) == 1)
{
// When first bit is active
result = result ^ 1;
}
n /= 2;
}
// Display given number
print(" Number : ", num, terminator: "");
// After the reversing bits
print("\n Output : ", result ,"\n");
}
}
func main()
{
let task: InvertBits = InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}
main();``````

#### Output

`````` Number :  16
Output :  1

Number :  35
Output :  49

Number :  20
Output :  5
``````
``````/*
Kotlin Program for
Reverse the bits of a number
*/
class InvertBits
{
// Reverse the bits element of a number
fun reverseBits(num: Int): Unit
{
var result: Int = 0;
var n: Int = num;
// Execute loop until n is less than 1
while (n > 0)
{
if (result != 0)
{
result = result shl 1;
}
if ((n and 1) == 1)
{
// When first bit is active
result = result xor 1;
}
n /= 2;
}
// Display given number
print(" Number : " + num);
// After the reversing bits
print("\n Output : " + result + "\n\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: InvertBits = InvertBits();
// Test cases
// 10000 => 00001 (1)
// 100011 => 110001 (49)
// 10100  => 00101 (5)
}``````

#### Output

`````` Number : 16
Output : 1

Number : 35
Output : 49

Number : 20
Output : 5
``````

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.