Turn on a rightmost unset bit

Here given code implementation process.

// C Program 
// Turn on a rightmost unset bit
#include <stdio.h>

void changeRightMostUnset(int num)
{
	if (num <= 0)
	{
		return;
	}
	int v = 1;
	int result = num;
	// Find value of first rightmost unset bit
	while (v < num && (v & num) == v)
	{
		v = v << 1;
	}
	if (v < num)
	{
		// Active unset bit
		result += v;
	}
	printf("\n Number : %d \n", num);
	// Display calculated result
	printf(" Result : %d\n", result);
}
int main()
{
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	changeRightMostUnset(7);
	return 0;
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
/*
    Java Program
    Turn on a rightmost unset bit
*/
public class BitManipulation
{
	public void changeRightMostUnset(int num)
	{
		if (num <= 0)
		{
			return;
		}
		int v = 1;
		int result = num;
		// Find value of first rightmost unset bit
		while (v < num && (v & num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		System.out.println("\n Number : " + num);
		// Display calculated result
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		// Test A
		// num = 321  
		// 321 = (101000001)
		//               ↑   Change bit
		// ---------------------
		//        101000011)
		// Result : 323
		task.changeRightMostUnset(321);
		// Test B 
		// (616) = (1001101000) 
		//                   ↑      Change bit
		//          0110011001
		// ---------------------
		// Result : 617
		task.changeRightMostUnset(616);
		// Test C
		// (55) = (110111)
		//           ↑      Change bit
		//         111111
		// ---------------------
		// Result : 63
		task.changeRightMostUnset(55);
		// Test D
		// 11   = (1011)  
		//          ↑   Change bit
		//         1111
		// ---------------------
		// Result : 15
		task.changeRightMostUnset(11);
		// Test E
		// 7    = (111)  
		//         111
		// ---------------------
		// Result : 7
		task.changeRightMostUnset(7);
	}
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Turn on a rightmost unset bit
*/
class BitManipulation
{
	public: void changeRightMostUnset(int num)
	{
		if (num <= 0)
		{
			return;
		}
		int v = 1;
		int result = num;
		// Find value of first rightmost unset bit
		while (v < num && (v &num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		cout << "\n Number : " << num << endl;
		// Display calculated result
		cout << " Result : " << result << endl;
	}
};
int main()
{
	BitManipulation *task = new BitManipulation();
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	task->changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	task->changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	task->changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	task->changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	task->changeRightMostUnset(7);
	return 0;
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
// Include namespace system
using System;
/*
    Csharp Program
    Turn on a rightmost unset bit
*/
public class BitManipulation
{
	public void changeRightMostUnset(int num)
	{
		if (num <= 0)
		{
			return;
		}
		int v = 1;
		int result = num;
		// Find value of first rightmost unset bit
		while (v < num && (v & num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		Console.WriteLine("\n Number : " + num);
		// Display calculated result
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		// Test A
		// num = 321  
		// 321 = (101000001)
		//               ↑   Change bit
		// ---------------------
		//        101000011)
		// Result : 323
		task.changeRightMostUnset(321);
		// Test B 
		// (616) = (1001101000) 
		//                   ↑      Change bit
		//          0110011001
		// ---------------------
		// Result : 617
		task.changeRightMostUnset(616);
		// Test C
		// (55) = (110111)
		//           ↑      Change bit
		//         111111
		// ---------------------
		// Result : 63
		task.changeRightMostUnset(55);
		// Test D
		// 11   = (1011)  
		//          ↑   Change bit
		//         1111
		// ---------------------
		// Result : 15
		task.changeRightMostUnset(11);
		// Test E
		// 7    = (111)  
		//         111
		// ---------------------
		// Result : 7
		task.changeRightMostUnset(7);
	}
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
package main
import "fmt"
/*
    Go Program
    Turn on a rightmost unset bit
*/
type BitManipulation struct {}
func getBitManipulation() * BitManipulation {
	var me *BitManipulation = &BitManipulation {}
	return me
}
func(this BitManipulation) changeRightMostUnset(num int) {
	if num <= 0 {
		return
	}
	var v int = 1
	var result int = num
	// Find value of first rightmost unset bit
	for (v < num && (v & num) == v) {
		v = v << 1
	}
	if v < num {
		// Active unset bit
		result += v
	}
	fmt.Println("\n Number : ", num)
	// Display calculated result
	fmt.Println(" Result : ", result)
}
func main() {
	var task * BitManipulation = getBitManipulation()
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	task.changeRightMostUnset(321)
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	task.changeRightMostUnset(616)
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	task.changeRightMostUnset(55)
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	task.changeRightMostUnset(11)
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	task.changeRightMostUnset(7)
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
<?php
/*
    Php Program
    Turn on a rightmost unset bit
*/
class BitManipulation
{
	public	function changeRightMostUnset($num)
	{
		if ($num <= 0)
		{
			return;
		}
		$v = 1;
		$result = $num;
		// Find value of first rightmost unset bit
		while ($v < $num && ($v & $num) == $v)
		{
			$v = $v << 1;
		}
		if ($v < $num)
		{
			// Active unset bit
			$result += $v;
		}
		echo("\n Number : ".$num."\n");
		// Display calculated result
		echo(" Result : ".$result."\n");
	}
}

function main()
{
	$task = new BitManipulation();
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	$task->changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	$task->changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	$task->changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	$task->changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	$task->changeRightMostUnset(7);
}
main();

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
/*
    Node JS Program
    Turn on a rightmost unset bit
*/
class BitManipulation
{
	changeRightMostUnset(num)
	{
		if (num <= 0)
		{
			return;
		}
		var v = 1;
		var result = num;
		// Find value of first rightmost unset bit
		while (v < num && (v & num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		console.log("\n Number : " + num);
		// Display calculated result
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new BitManipulation();
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	task.changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	task.changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	task.changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	task.changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	task.changeRightMostUnset(7);
}
main();

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
#    Python 3 Program
#    Turn on a rightmost unset bit
class BitManipulation :
	def changeRightMostUnset(self, num) :
		if (num <= 0) :
			return
		
		v = 1
		result = num
		#  Find value of first rightmost unset bit
		while (v < num and(v & num) == v) :
			v = v << 1
		
		if (v < num) :
			#  Active unset bit
			result += v
		
		print("\n Number : ", num)
		#  Display calculated result
		print(" Result : ", result)
	

def main() :
	task = BitManipulation()
	#  Test A
	#  num = 321  
	#  321 = (101000001)
	#                ↑   Change bit
	#  ---------------------
	#         101000011)
	#  Result : 323
	task.changeRightMostUnset(321)
	#  Test B 
	#  (616) = (1001101000) 
	#                    ↑      Change bit
	#           0110011001
	#  ---------------------
	#  Result : 617
	task.changeRightMostUnset(616)
	#  Test C
	#  (55) = (110111)
	#            ↑      Change bit
	#          111111
	#  ---------------------
	#  Result : 63
	task.changeRightMostUnset(55)
	#  Test D
	#  11   = (1011)  
	#           ↑   Change bit
	#          1111
	#  ---------------------
	#  Result : 15
	task.changeRightMostUnset(11)
	#  Test E
	#  7    = (111)  
	#          111
	#  ---------------------
	#  Result : 7
	task.changeRightMostUnset(7)

if __name__ == "__main__": main()

Output

 Number :  321
 Result :  323

 Number :  616
 Result :  617

 Number :  55
 Result :  63

 Number :  11
 Result :  15

 Number :  7
 Result :  7
#    Ruby Program
#    Turn on a rightmost unset bit
class BitManipulation 
	def changeRightMostUnset(num) 
		if (num <= 0) 
			return
		end

		v = 1
		result = num
		#  Find value of first rightmost unset bit
		while (v < num && (v & num) == v) 
			v = v << 1
		end

		if (v < num) 
			#  Active unset bit
			result += v
		end

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

end

def main() 
	task = BitManipulation.new()
	#  Test A
	#  num = 321  
	#  321 = (101000001)
	#                ↑   Change bit
	#  ---------------------
	#         101000011)
	#  Result : 323
	task.changeRightMostUnset(321)
	#  Test B 
	#  (616) = (1001101000) 
	#                    ↑      Change bit
	#           0110011001
	#  ---------------------
	#  Result : 617
	task.changeRightMostUnset(616)
	#  Test C
	#  (55) = (110111)
	#            ↑      Change bit
	#          111111
	#  ---------------------
	#  Result : 63
	task.changeRightMostUnset(55)
	#  Test D
	#  11   = (1011)  
	#           ↑   Change bit
	#          1111
	#  ---------------------
	#  Result : 15
	task.changeRightMostUnset(11)
	#  Test E
	#  7    = (111)  
	#          111
	#  ---------------------
	#  Result : 7
	task.changeRightMostUnset(7)
end

main()

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
/*
    Scala Program
    Turn on a rightmost unset bit
*/
class BitManipulation()
{
	def changeRightMostUnset(num: Int): Unit = {
		if (num <= 0)
		{
			return;
		}
		var v: Int = 1;
		var result: Int = num;
		// Find value of first rightmost unset bit
		while (v < num && (v & num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		println("\n Number : " + num);
		// Display calculated result
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitManipulation = new BitManipulation();
		// Test A
		// num = 321  
		// 321 = (101000001)
		//               ↑   Change bit
		// ---------------------
		//        101000011)
		// Result : 323
		task.changeRightMostUnset(321);
		// Test B 
		// (616) = (1001101000) 
		//                   ↑      Change bit
		//          0110011001
		// ---------------------
		// Result : 617
		task.changeRightMostUnset(616);
		// Test C
		// (55) = (110111)
		//           ↑      Change bit
		//         111111
		// ---------------------
		// Result : 63
		task.changeRightMostUnset(55);
		// Test D
		// 11   = (1011)  
		//          ↑   Change bit
		//         1111
		// ---------------------
		// Result : 15
		task.changeRightMostUnset(11);
		// Test E
		// 7    = (111)  
		//         111
		// ---------------------
		// Result : 7
		task.changeRightMostUnset(7);
	}
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7
/*
    Swift 4 Program
    Turn on a rightmost unset bit
*/
class BitManipulation
{
	func changeRightMostUnset(_ num: Int)
	{
		if (num <= 0)
		{
			return;
		}
		var v: Int = 1;
		var result: Int = num;
		// Find value of first rightmost unset bit
		while (v < num && (v & num) == v)
		{
			v = v << 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		print("\n Number : ", num);
		// Display calculated result
		print(" Result : ", result);
	}
}
func main()
{
	let task: BitManipulation = BitManipulation();
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	task.changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	task.changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	task.changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	task.changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	task.changeRightMostUnset(7);
}
main();

Output

 Number :  321
 Result :  323

 Number :  616
 Result :  617

 Number :  55
 Result :  63

 Number :  11
 Result :  15

 Number :  7
 Result :  7
/*
    Kotlin Program
    Turn on a rightmost unset bit
*/
class BitManipulation
{
	fun changeRightMostUnset(num: Int): Unit
	{
		if (num <= 0)
		{
			return;
		}
		var v: Int = 1;
		var result: Int = num;
		// Find value of first rightmost unset bit
		while (v < num && (v and num) == v)
		{
			v = v shl 1;
		}
		if (v < num)
		{
			// Active unset bit
			result += v;
		}
		println("\n Number : " + num);
		// Display calculated result
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BitManipulation = BitManipulation();
	// Test A
	// num = 321  
	// 321 = (101000001)
	//               ↑   Change bit
	// ---------------------
	//        101000011)
	// Result : 323
	task.changeRightMostUnset(321);
	// Test B 
	// (616) = (1001101000) 
	//                   ↑      Change bit
	//          0110011001
	// ---------------------
	// Result : 617
	task.changeRightMostUnset(616);
	// Test C
	// (55) = (110111)
	//           ↑      Change bit
	//         111111
	// ---------------------
	// Result : 63
	task.changeRightMostUnset(55);
	// Test D
	// 11   = (1011)  
	//          ↑   Change bit
	//         1111
	// ---------------------
	// Result : 15
	task.changeRightMostUnset(11);
	// Test E
	// 7    = (111)  
	//         111
	// ---------------------
	// Result : 7
	task.changeRightMostUnset(7);
}

Output

 Number : 321
 Result : 323

 Number : 616
 Result : 617

 Number : 55
 Result : 63

 Number : 11
 Result : 15

 Number : 7
 Result : 7

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







© 2021, kalkicode.com, All rights reserved