Change all odd position bit of a number

Here given code implementation process.

// C Program 
// Change all odd bits of a number
#include <stdio.h>

// Change odd position bit of a given number
void changeOddBit(int num)
{
	int n = num;
	int start = 1;
	while (n >= start)
	{
		// Change bits
		n = start ^ n;
		// Shift bits to left by 2 bit
		start = start << 2;
	}
	// Display calculated result
	printf("\n Number : %d", num);
	printf("\n After Change Odd Position Bits : %d", n);
}
int main()
{
	// 1000 => 0101
	// Change odd position bit
	changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	changeOddBit(123);
	return 0;
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
/*
  Java Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	public void changeOddBit(int num)
	{
		int n = num;
		int start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by 2 bit
			start = start << 2;
		}
		// Display calculated result
		System.out.print("\n Number : " + num);
		System.out.print("\n After Change Odd Position Bits : " + n);
	}
	public static void main(String[] args)
	{
		SwitchBit task = new SwitchBit();
		// 1000 => 0101
		// Change odd position bit
		task.changeOddBit(16);
		// 1111011 => 0101110 [Change Odd position]
		task.changeOddBit(123);
	}
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Change all odd bits of a number
*/

class SwitchBit
{
	public:
		// Change odd position bit of a given number
		void changeOddBit(int num)
		{
			int n = num;
			int start = 1;
			while (n >= start)
			{
				// Change bits
				n = start ^ n;
				// Shift bits to left by 2 bit
				start = start << 2;
			}
			// Display calculated result
			cout << "\n Number : " << num;
			cout << "\n After Change Odd Position Bits : " << n;
		}
};
int main()
{
	SwitchBit task = SwitchBit();
	// 1000 => 0101
	// Change odd position bit
	task.changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123);
	return 0;
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
// Include namespace system
using System;
/*
  C# Program for
  Change all odd bits of a number
*/
public class SwitchBit
{
	// Change odd position bit of a given number
	public void changeOddBit(int num)
	{
		int n = num;
		int start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by 2 bit
			start = start << 2;
		}
		// Display calculated result
		Console.Write("\n Number : " + num);
		Console.Write("\n After Change Odd Position Bits : " + n);
	}
	public static void Main(String[] args)
	{
		SwitchBit task = new SwitchBit();
		// 1000 => 0101
		// Change odd position bit
		task.changeOddBit(16);
		// 1111011 => 0101110 [Change Odd position]
		task.changeOddBit(123);
	}
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
<?php
/*
  Php Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	public	function changeOddBit($num)
	{
		$n = $num;
		$start = 1;
		while ($n >= $start)
		{
			// Change bits
			$n = $start ^ $n;
			// Shift bits to left by 2 bit
			$start = $start << 2;
		}
		// Display calculated result
		echo "\n Number : ". $num;
		echo "\n After Change Odd Position Bits : ". $n;
	}
}

function main()
{
	$task = new SwitchBit();
	// 1000 => 0101
	// Change odd position bit
	$task->changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	$task->changeOddBit(123);
}
main();

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
/*
  Node Js Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	changeOddBit(num)
	{
		var n = num;
		var start = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by 2 bit
			start = start << 2;
		}
		// Display calculated result
		process.stdout.write("\n Number : " + num);
		process.stdout.write("\n After Change Odd Position Bits : " + n);
	}
}

function main()
{
	var task = new SwitchBit();
	// 1000 => 0101
	// Change odd position bit
	task.changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123);
}
main();

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
#   Python 3 Program for
#   Change all odd bits of a number

class SwitchBit :
	#  Change odd position bit of a given number
	def changeOddBit(self, num) :
		n = num
		start = 1
		while (n >= start) :
			#  Change bits
			n = start ^ n
			#  Shift bits to left by 2 bit
			start = start << 2
		
		#  Display calculated result
		print("\n Number : ", num, end = "")
		print("\n After Change Odd Position Bits : ", n, end = "")
	

def main() :
	task = SwitchBit()
	#  1000 => 0101
	#  Change odd position bit
	task.changeOddBit(16)
	#  1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123)

if __name__ == "__main__": main()

Output

 Number :  16
 After Change Odd Position Bits :  5
 Number :  123
 After Change Odd Position Bits :  46
#   Ruby Program for
#   Change all odd bits of a number

class SwitchBit 
	#  Change odd position bit of a given number
	def changeOddBit(num) 
		n = num
		start = 1
		while (n >= start) 
			#  Change bits
			n = start ^ n
			#  Shift bits to left by 2 bit
			start = start << 2
		end

		#  Display calculated result
		print("\n Number : ", num)
		print("\n After Change Odd Position Bits : ", n)
	end

end

def main() 
	task = SwitchBit.new()
	#  1000 => 0101
	#  Change odd position bit
	task.changeOddBit(16)
	#  1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123)
end

main()

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
/*
  Scala Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	def changeOddBit(num: Int): Unit = {
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by 2 bit
			start = start << 2;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n After Change Odd Position Bits : " + n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SwitchBit = new SwitchBit();
		// 1000 => 0101
		// Change odd position bit
		task.changeOddBit(16);
		// 1111011 => 0101110 [Change Odd position]
		task.changeOddBit(123);
	}
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46
/*
  Swift 4 Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	func changeOddBit(_ num: Int)
	{
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start ^ n;
			// Shift bits to left by 2 bit
			start = start << 2;
		}
		// Display calculated result
		print("\n Number : ", num, terminator: "");
		print("\n After Change Odd Position Bits : ", n, terminator: "");
	}
}
func main()
{
	let task: SwitchBit = SwitchBit();
	// 1000 => 0101
	// Change odd position bit
	task.changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123);
}
main();

Output

 Number :  16
 After Change Odd Position Bits :  5
 Number :  123
 After Change Odd Position Bits :  46
/*
  Kotlin Program for
  Change all odd bits of a number
*/
class SwitchBit
{
	// Change odd position bit of a given number
	fun changeOddBit(num: Int): Unit
	{
		var n: Int = num;
		var start: Int = 1;
		while (n >= start)
		{
			// Change bits
			n = start xor n;
			// Shift bits to left by 2 bit
			start = start shl 2;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n After Change Odd Position Bits : " + n);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SwitchBit = SwitchBit();
	// 1000 => 0101
	// Change odd position bit
	task.changeOddBit(16);
	// 1111011 => 0101110 [Change Odd position]
	task.changeOddBit(123);
}

Output

 Number : 16
 After Change Odd Position Bits : 5
 Number : 123
 After Change Odd Position Bits : 46


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