Swap all even and odd bits of a number

Here given code implementation process.

// C Program 
// Swap all odd and even bits
#include <stdio.h>

// Swap two bits in a given number
void swapEvenOddBits(int n)
{
	// Get all even active bits
	int even = (n & 2863311530) >> 1;
	// Get all Odd active bits
	int odd = (n & 1431655765) << 1;
	int result = even | odd;
	// Display calculated result
	printf(" Number : %d", n);
	printf("\n Output : %d\n\n", result);
}
int main()
{
	// Test cases
	// 010000 => 100000 
	swapEvenOddBits(16);
	// 00100011 (35) =>  00010011 (19)
	swapEvenOddBits(35);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  1111110001 (-15) => 11110010 (-14) 
	swapEvenOddBits(-15);
	// 1010 (10) => 0101
	swapEvenOddBits(10);
	return 0;
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
/*
  Java Program for
  Swap all odd and even bits
*/
public class BitExchange
{
	// Swap two bits in a given number
	public void swapEvenOddBits(int n)
	{
		// Get all even active bits
		long even = (n & 2863311530L) >> 1;
		// Get all Odd active bits
		long odd =  (n & 1431655765) << 1;
		long result = even | odd;
		// Display calculated result
		System.out.print(" Number : " + n);
		System.out.print("\n Output : " + result + "\n\n");
	}
	public static void main(String[] args)
	{
		BitExchange task = new BitExchange();
		//  Test cases
		//  010000 => 100000 (32)
		task.swapEvenOddBits(16);
		//  00100011 (35) =>  00010011 (19)
		task.swapEvenOddBits(35);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  1111110001 (-15) => 11110010 (-14) 
		task.swapEvenOddBits(-15);
		//  1010 (10) => 0101
		task.swapEvenOddBits(10);
	}
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Swap all odd and even bits
*/

class BitExchange
{
	public:
		// Swap two bits in a given number
		void swapEvenOddBits(int n)
		{
			// Get all even active bits
			int even = (n & 2863311530) >> 1;
			// Get all Odd active bits
			int odd = (n & 1431655765) << 1;
			int result = even | odd;
			// Display calculated result
			cout << " Number : " << n;
			cout << "\n Output : " << result << "\n\n";
		}
};
int main()
{
	BitExchange task = BitExchange();
	//  Test cases
	//  010000 => 100000 (32)
	task.swapEvenOddBits(16);
	//  00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  1111110001 (-15) => 11110010 (-14)
	task.swapEvenOddBits(-15);
	//  1010 (10) => 0101
	task.swapEvenOddBits(10);
	return 0;
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
// Include namespace system
using System;
/*
  C# Program for
  Swap all odd and even bits
*/
public class BitExchange
{
	// Swap two bits in a given number
	public void swapEvenOddBits(int n)
	{
		// Get all even active bits
		long even = (n & 2863311530) >> 1;
		// Get all Odd active bits
		long odd = (n & 1431655765) << 1;
		long result = even | odd;
		// Display calculated result
		Console.Write(" Number : " + n);
		Console.Write("\n Output : " + result + "\n\n");
	}
	public static void Main(String[] args)
	{
		BitExchange task = new BitExchange();
		//  Test cases
		//  010000 => 100000 (32)
		task.swapEvenOddBits(16);
		//  00100011 (35) =>  00010011 (19)
		task.swapEvenOddBits(35);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  1111110001 (-15) => 11110010 (-14)
		task.swapEvenOddBits(-15);
		//  1010 (10) => 0101
		task.swapEvenOddBits(10);
	}
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
<?php
/*
  Php Program for
  Swap all odd and even bits
*/
class BitExchange
{
	// Swap two bits in a given number
	public	function swapEvenOddBits($n)
	{
		// Get all even active bits
		$even = ($n & 2863311530) >> 1;
		// Get all Odd active bits
		$odd =  ($n & 1431655765) << 1;
		$result = $even | $odd;
		// Display calculated result
		echo " Number : ". $n;
		echo "\n Output : ". $result ."\n\n";
	}
}

function main()
{
	$task = new BitExchange();
	//  Test cases
	//  010000 => 100000 (32)
	$task->swapEvenOddBits(16);
	//  00100011 (35) =>  00010011 (19)
	$task->swapEvenOddBits(35);
	//  1010 (10) => 0101
	$task->swapEvenOddBits(10);
}
main();

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : 10
 Output : 5
/*
  Node Js Program for
  Swap all odd and even bits
*/
class BitExchange
{
	// Swap two bits in a given number
	swapEvenOddBits(n)
	{
		// Get all even active bits
		var even = (n & 2863311530) >> 1;
		// Get all Odd active bits
		var odd = (n & 1431655765) << 1;
		var result = even | odd;
		// Display calculated result
		process.stdout.write(" Number : " + n);
		process.stdout.write("\n Output : " + result + "\n\n");
	}
}

function main()
{
	var task = new BitExchange();
	//  Test cases
	//  010000 => 100000 (32)
	task.swapEvenOddBits(16);
	//  00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  1111110001 (-15) => 11110010 (-14)
	task.swapEvenOddBits(-15);
	//  1010 (10) => 0101
	task.swapEvenOddBits(10);
}
main();

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
#   Python 3 Program for
#   Swap all odd and even bits

class BitExchange :
	#  Swap two bits in a given number
	def swapEvenOddBits(self, n) :
		#  Get all even active bits
		even = (n & 2863311530) >> 1
		#  Get all Odd active bits
		odd = (n & 1431655765) << 1
		result = even | odd
		#  Display calculated result
		print(" Number : ", n, end = "")
		print("\n Output : ", result ,"\n")
	

def main() :
	task = BitExchange()
	#   Test cases
	#   010000 => 100000 (32)
	task.swapEvenOddBits(16)
	#   00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35)
	#   1010 (10) => 0101
	task.swapEvenOddBits(10)

if __name__ == "__main__": main()

Output

 Number :  16
 Output :  32

 Number :  35
 Output :  19

 Number :  10
 Output :  5
#   Ruby Program for
#   Swap all odd and even bits

class BitExchange 
	#  Swap two bits in a given number
	def swapEvenOddBits(n) 
		#  Get all even active bits
		even = (n & 2863311530) >> 1
		#  Get all Odd active bits
		odd = (n & 1431655765) << 1
		result = even | odd
		#  Display calculated result
		print(" Number : ", n)
		print("\n Output : ", result ,"\n\n")
	end

end

def main() 
	task = BitExchange.new()
	#   Test cases
	#   010000 => 100000 (32)
	task.swapEvenOddBits(16)
	#   00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35)
	#   1010 (10) => 0101
	task.swapEvenOddBits(10)
end

main()

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : 10
 Output : 5

/*
  Scala Program for
  Swap all odd and even bits
*/
class BitExchange
{
	// Swap two bits in a given number
	def swapEvenOddBits(n: Int): Unit = {
		// Get all even active bits
		var even: Long = (n & 2863311530L) >> 1;
		// Get all Odd active bits
		var odd: Long = (n & 1431655765) << 1;
		var result: Long = even | odd;
		// Display calculated result
		print(" Number : " + n);
		print("\n Output : " + result + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitExchange = new BitExchange();
		//  Test cases
		//  010000 => 100000 (32)
		task.swapEvenOddBits(16);
		//  00100011 (35) =>  00010011 (19)
		task.swapEvenOddBits(35);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  1111110001 (-15) => 11110010 (-14)
		task.swapEvenOddBits(-15);
		//  1010 (10) => 0101
		task.swapEvenOddBits(10);
	}
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : -15
 Output : -14

 Number : 10
 Output : 5
/*
  Swift 4 Program for
  Swap all odd and even bits
*/
class BitExchange
{
	// Swap two bits in a given number
	func swapEvenOddBits(_ n: Int)
	{
		// Get all even active bits
		let even: Int = (n & 2863311530) >> 1;
		// Get all Odd active bits
		let odd: Int = (n & 1431655765) << 1;
		let result: Int = even | odd;
		// Display calculated result
		print(" Number : ", n, terminator: "");
		print("\n Output : ", result ,"\n");
	}
}
func main()
{
	let task: BitExchange = BitExchange();
	//  Test cases
	//  010000 => 100000 (32)
	task.swapEvenOddBits(16);
	//  00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35);
	//  1010 (10) => 0101
	task.swapEvenOddBits(10);
}
main();

Output

 Number :  16
 Output :  32

 Number :  35
 Output :  19

 Number :  10
 Output :  5
/*
  Kotlin Program for
  Swap all odd and even bits
*/
class BitExchange
{
	// Swap two bits in a given number
	fun swapEvenOddBits(n: Long): Unit
	{
		// Get all even active bits
		var even: Long = (n and 2863311530) shr 1;
		// Get all Odd active bits
		var odd: Long = (n and 1431655765) shl 1;
		var result: Long = even or odd;
		// Display calculated result
		print(" Number : " + n);
		print("\n Output : " + result + "\n\n");
	}
}
fun main(args: Array <String> ): Unit
{
	var task: BitExchange = BitExchange();
	//  Test cases
	//  010000 => 100000 (32)
	task.swapEvenOddBits(16);
	//  00100011 (35) =>  00010011 (19)
	task.swapEvenOddBits(35);
	//  1010 (10) => 0101
	task.swapEvenOddBits(10);
}

Output

 Number : 16
 Output : 32

 Number : 35
 Output : 19

 Number : 10
 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.

New Comment







© 2021, kalkicode.com, All rights reserved