# Set the leftmost unset bit

Here given code implementation process.

``````// C Program
// Set the leftmost unset bit
#include <stdio.h>

// Remove a most significant bit of given number
void changeBit(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);
// Find most significant set bit
int value = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
printf(" Number : %d \n", num);
printf(" Result : %d\n", value);
}
int main()
{
// Test A
// 320 = (101000000)
//         ↑        Change left bit
// ---------------------
//        111000000)
// Result : 448
changeBit(320);
// Test B
// (1000) = (1111101000)
//                ↑     Change left bit
//           1111111000
// ---------------------
// Result : 1016
changeBit(1000);
// Test C
// (54) = (110110)
//           ↑      Change left bit
//         111110
// ---------------------
// Result : 62
changeBit(54);
// Test D
// 9    = (1001)
//          ↑    Change left bit
//         1101
// ---------------------
// Result : 13
changeBit(9);
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
changeBit(7);
return 0;
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````/*
Java Program
Set the leftmost unset bit
*/
public class BitManipulation
{
// Remove a most significant bit of given number
public void changeBit(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);
// Find most significant set bit
int value = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
System.out.print(" Number : " + num + " \n");
System.out.print(" Result : " + value + "\n");
}
public static void main(String[] args)
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Set the leftmost unset bit
*/
class BitManipulation
{
public:
// Remove a most significant bit of given number
void changeBit(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);
// Find most significant set bit
int value = (r + 1) >> 1;
while (value > 0 && (value &num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
cout << " Number : " << num << " \n";
cout << " Result : " << value << "\n";
}
};
int main()
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
return 0;
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````// Include namespace system
using System;
/*
Csharp Program
Set the leftmost unset bit
*/
public class BitManipulation
{
// Remove a most significant bit of given number
public void changeBit(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);
// Find most significant set bit
int value = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
Console.Write(" Number : " + num + " \n");
Console.Write(" Result : " + value + "\n");
}
public static void Main(String[] args)
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````package main
import "fmt"
/*
Go Program
Set the leftmost unset bit
*/
type BitManipulation struct {}
func getBitManipulation() * BitManipulation {
var me *BitManipulation = &BitManipulation {}
return me
}
// Remove a most significant bit of given number
func(this BitManipulation) changeBit(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)
// Find most significant set bit
var value int = (r + 1) >> 1
for (value > 0 && (value & num) == value) {
value = value >> 1
}
// Combine result
value = value + num
// Display calculated result
fmt.Print(" Number : ", num, " \n")
fmt.Print(" Result : ", value, "\n")
}
func main() {
var task * BitManipulation = getBitManipulation()
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````<?php
/*
Php Program
Set the leftmost unset bit
*/
class BitManipulation
{
// Remove a most significant bit of given number
public	function changeBit(\$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);
// Find most significant set bit
\$value = (\$r + 1) >> 1;
while (\$value > 0 && (\$value & \$num) == \$value)
{
\$value = \$value >> 1;
}
// Combine result
\$value = \$value + \$num;
// Display calculated result
echo(" Number : ".\$num." \n");
echo(" Result : ".\$value."\n");
}
}

function main()
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
main();``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````/*
Node JS Program
Set the leftmost unset bit
*/
class BitManipulation
{
// Remove a most significant bit of given number
changeBit(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);
// Find most significant set bit
var value = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
process.stdout.write(" Number : " + num + " \n");
process.stdout.write(" Result : " + value + "\n");
}
}

function main()
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
main();``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````#    Python 3 Program
#    Set the leftmost unset bit
class BitManipulation :
#  Remove a most significant bit of given number
def changeBit(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)
#  Find most significant set bit
value = (r + 1) >> 1
while (value > 0 and(value & num) == value) :
value = value >> 1

#  Combine result
value = value + num
#  Display calculated result
print(" Number : ", num ," ")
print(" Result : ", value )

def main() :
#  Test A
#  320 = (101000000)
#          ↑        Change bit
#  ---------------------
#         111000000)
#  Result : 448
#  Test B
#  (1000) = (1111101000)
#                 ↑     Change bit
#            1111111000
#  ---------------------
#  Result : 1016
#  Test C
#  (54) = (110110)
#            ↑      Change bit
#          111110
#  ---------------------
#  Result : 62
#  Test D
#  9    = (1001)
#           ↑    Change bit
#          1101
#  ---------------------
#  Result : 13
#  Test E
#  7    = (111)
#              There is no inactive bit
#          111
#  ---------------------
#  Result : 7

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

#### Output

`````` Number :  320
Result :  448
Number :  1000
Result :  1016
Number :  54
Result :  62
Number :  9
Result :  13
Number :  7
Result :  7``````
``````#    Ruby Program
#    Set the leftmost unset bit
class BitManipulation
#  Remove a most significant bit of given number
def changeBit(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)
#  Find most significant set bit
value = (r + 1) >> 1
while (value > 0 && (value & num) == value)
value = value >> 1
end

#  Combine result
value = value + num
#  Display calculated result
print(" Number : ", num ," \n")
print(" Result : ", value ,"\n")
end

end

def main()
#  Test A
#  320 = (101000000)
#          ↑        Change bit
#  ---------------------
#         111000000)
#  Result : 448
#  Test B
#  (1000) = (1111101000)
#                 ↑     Change bit
#            1111111000
#  ---------------------
#  Result : 1016
#  Test C
#  (54) = (110110)
#            ↑      Change bit
#          111110
#  ---------------------
#  Result : 62
#  Test D
#  9    = (1001)
#           ↑    Change bit
#          1101
#  ---------------------
#  Result : 13
#  Test E
#  7    = (111)
#              There is no inactive bit
#          111
#  ---------------------
#  Result : 7
end

main()``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7
``````
``````/*
Scala Program
Set the leftmost unset bit
*/
class BitManipulation()
{
// Remove a most significant bit of given number
def changeBit(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);
// Find most significant set bit
var value: Int = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
print(" Number : " + num + " \n");
print(" Result : " + value + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitManipulation = new BitManipulation();
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````
``````/*
Swift 4 Program
Set the leftmost unset bit
*/
class BitManipulation
{
// Remove a most significant bit of given number
func changeBit(_ 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);
// Find most significant set bit
var value: Int = (r + 1) >> 1;
while (value > 0 && (value & num) == value)
{
value = value >> 1;
}
// Combine result
value = value + num;
// Display calculated result
print(" Number : ", num ," ");
print(" Result : ", value );
}
}
func main()
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}
main();``````

#### Output

`````` Number :  320
Result :  448
Number :  1000
Result :  1016
Number :  54
Result :  62
Number :  9
Result :  13
Number :  7
Result :  7``````
``````/*
Kotlin Program
Set the leftmost unset bit
*/
class BitManipulation
{
// Remove a most significant bit of given number
fun changeBit(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);
// Find most significant set bit
var value: Int = (r + 1) shr 1;
while (value > 0 && (value and num) == value)
{
value = value shr 1;
}
// Combine result
value = value + num;
// Display calculated result
print(" Number : " + num + " \n");
print(" Result : " + value + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// 320 = (101000000)
//         ↑        Change bit
// ---------------------
//        111000000)
// Result : 448
// Test B
// (1000) = (1111101000)
//                ↑     Change bit
//           1111111000
// ---------------------
// Result : 1016
// Test C
// (54) = (110110)
//           ↑      Change bit
//         111110
// ---------------------
// Result : 62
// Test D
// 9    = (1001)
//          ↑    Change bit
//         1101
// ---------------------
// Result : 13
// Test E
// 7    = (111)
//             There is no inactive bit
//         111
// ---------------------
// Result : 7
}``````

#### Output

`````` Number : 320
Result : 448
Number : 1000
Result : 1016
Number : 54
Result : 62
Number : 9
Result : 13
Number : 7
Result : 7``````

## 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.