Swap all adjacent bits in given number

Here given code implementation process.

// C program 
// Swap all adjacent bits in given number
#include <stdio.h>

// Swap the pair of adjacent bits
void swapBits(int num)
{
    // 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
    // 0x55555555 (01010101010101010101010101010101) (1431655765)
    // (num & 0xAAAAAAAA) Get the set bits in Even position of num
    // (num & 0x55555555) Get the set bits in Odd position of num
    // ((Even active bits) >> 1) shift 1 bits right side
    // ((Odd active bits) << 1)  shift 1 bits left side
    int result =  (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1));

    // Display given number
    printf("\n Number : %d",num);
    // Display calculated result 
    printf("\n Result : %d",result);
}
int main(int argc, char const *argv[])
{
    
    // (37)    100101
    //         10 01 01
    //         01 10 10
    // Result  (011010) (26)
    swapBits(37);
    // (216)    11011000
    //          11 01 10 00
    // Swap     11 10 01 00
    // Result  (11100100) (228)
    swapBits(216);
    // (42)     101010
    //          10 10 10
    // Swap     01 01 01
    // Result  (010101) (21)
    swapBits(42);


    return 0;
}

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
/*
  Java program
  Swap all adjacent bits in given number
*/
public class BitSwap
{
	// Swap the pair of adjacent bits
	public void swapBits(int num)
	{
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		int result = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1));
		// Display given number
		System.out.print("\n Number : " + num);
		// Display calculated result 
		System.out.print("\n Result : " + result);
	}
	public static void main(String[] args)
	{
		BitSwap task = new BitSwap();
		// (37)    100101
		//         10 01 01
		//         01 10 10
		// Result  (011010) (26)
		task.swapBits(37);
		// (216)    11011000
		//          11 01 10 00
		// Swap     11 10 01 00
		// Result  (11100100) (228)
		task.swapBits(216);
		// (42)     101010
		//          10 10 10
		// Swap     01 01 01
		// Result  (010101) (21)
		task.swapBits(42);
	}
}

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Swap all adjacent bits in given number
*/
class BitSwap
{
	public:
		// Swap the pair of adjacent bits
		void swapBits(int num)
		{
			// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
			// 0x55555555 (01010101010101010101010101010101) (1431655765)
			// (num &0xAAAAAAAA) Get the set bits in Even position of num
			// (num &0x55555555) Get the set bits in Odd position of num
			// ((Even active bits) >> 1) shift 1 bits right side
			// ((Odd active bits) << 1)  shift 1 bits left side
			int result = (((num &0xAAAAAAAA) >> 1) | ((num &0x55555555) << 1));
			// Display given number
			cout << "\n Number : " << num;
			// Display calculated result
			cout << "\n Result : " << result;
		}
};
int main()
{
	BitSwap task = BitSwap();
	// (37)    100101
	//         10 01 01
	//         01 10 10
	// Result  (011010) (26)
	task.swapBits(37);
	// (216)    11011000
	//          11 01 10 00
	// Swap     11 10 01 00
	// Result  (11100100) (228)
	task.swapBits(216);
	// (42)     101010
	//          10 10 10
	// Swap     01 01 01
	// Result  (010101) (21)
	task.swapBits(42);
	return 0;
}

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
// Include namespace system
using System;
/*
  C# program
  Swap all adjacent bits in given number
*/
public class BitSwap
{
	// Swap the pair of adjacent bits
	public void swapBits(int num)
	{
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		long result = (((num & 0xAAAAAAAAL) >> 1) | ((num & 0x55555555L) << 1));
		// Display given number
		Console.Write("\n Number : " + num);
		// Display calculated result
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		BitSwap task = new BitSwap();
		// (37)    100101
		//         10 01 01
		//         01 10 10
		// Result  (011010) (26)
		task.swapBits(37);
		// (216)    11011000
		//          11 01 10 00
		// Swap     11 10 01 00
		// Result  (11100100) (228)
		task.swapBits(216);
		// (42)     101010
		//          10 10 10
		// Swap     01 01 01
		// Result  (010101) (21)
		task.swapBits(42);
	}
}

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
<?php
/*
  Php program
  Swap all adjacent bits in given number
*/
class BitSwap
{
	// Swap the pair of adjacent bits
	public	function swapBits($num)
	{
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		$result = ((($num & 0xAAAAAAAA) >> 1) | (($num & 0x55555555) << 1));
		// Display given number
		echo "\n Number : ". $num;
		// Display calculated result
		echo "\n Result : ". $result;
	}
}

function main()
{
	$task = new BitSwap();
	// (37)    100101
	//         10 01 01
	//         01 10 10
	// Result  (011010) (26)
	$task->swapBits(37);
	// (216)    11011000
	//          11 01 10 00
	// Swap     11 10 01 00
	// Result  (11100100) (228)
	$task->swapBits(216);
	// (42)     101010
	//          10 10 10
	// Swap     01 01 01
	// Result  (010101) (21)
	$task->swapBits(42);
}
main();

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
/*
  Node Js program
  Swap all adjacent bits in given number
*/
class BitSwap
{
	// Swap the pair of adjacent bits
	swapBits(num)
	{
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		var result = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1));
		// Display given number
		process.stdout.write("\n Number : " + num);
		// Display calculated result
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var task = new BitSwap();
	// (37)    100101
	//         10 01 01
	//         01 10 10
	// Result  (011010) (26)
	task.swapBits(37);
	// (216)    11011000
	//          11 01 10 00
	// Swap     11 10 01 00
	// Result  (11100100) (228)
	task.swapBits(216);
	// (42)     101010
	//          10 10 10
	// Swap     01 01 01
	// Result  (010101) (21)
	task.swapBits(42);
}
main();

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
#   Python 3 program
#   Swap all adjacent bits in given number

class BitSwap :
	#  Swap the pair of adjacent bits
	def swapBits(self, num) :
		#  0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		#  0x55555555 (01010101010101010101010101010101) (1431655765)
		#  (num & 0xAAAAAAAA) Get the set bits in Even position of num
		#  (num & 0x55555555) Get the set bits in Odd position of num
		#  ((Even active bits) >> 1) shift 1 bits right side
		#  ((Odd active bits) << 1)  shift 1 bits left side
		result = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1))
		#  Display given number
		print("\n Number : ", num, end = "")
		#  Display calculated result 
		print("\n Result : ", result, end = "")
	

def main() :
	task = BitSwap()
	#  (37)    100101
	#          10 01 01
	#          01 10 10
	#  Result  (011010) (26)
	task.swapBits(37)
	#  (216)    11011000
	#           11 01 10 00
	#  Swap     11 10 01 00
	#  Result  (11100100) (228)
	task.swapBits(216)
	#  (42)     101010
	#           10 10 10
	#  Swap     01 01 01
	#  Result  (010101) (21)
	task.swapBits(42)

if __name__ == "__main__": main()

Output

 Number :  37
 Result :  26
 Number :  216
 Result :  228
 Number :  42
 Result :  21
#   Ruby program
#   Swap all adjacent bits in given number

class BitSwap 
	#  Swap the pair of adjacent bits
	def swapBits(num) 
		#  0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		#  0x55555555 (01010101010101010101010101010101) (1431655765)
		#  (num & 0xAAAAAAAA) Get the set bits in Even position of num
		#  (num & 0x55555555) Get the set bits in Odd position of num
		#  ((Even active bits) >> 1) shift 1 bits right side
		#  ((Odd active bits) << 1)  shift 1 bits left side
		result = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1))
		#  Display given number
		print("\n Number : ", num)
		#  Display calculated result 
		print("\n Result : ", result)
	end

end

def main() 
	task = BitSwap.new()
	#  (37)    100101
	#          10 01 01
	#          01 10 10
	#  Result  (011010) (26)
	task.swapBits(37)
	#  (216)    11011000
	#           11 01 10 00
	#  Swap     11 10 01 00
	#  Result  (11100100) (228)
	task.swapBits(216)
	#  (42)     101010
	#           10 10 10
	#  Swap     01 01 01
	#  Result  (010101) (21)
	task.swapBits(42)
end

main()

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
/*
  Scala program
  Swap all adjacent bits in given number
*/
class BitSwap
{
	// Swap the pair of adjacent bits
	def swapBits(num: Int): Unit = {
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		var result: Int = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1));
		// Display given number
		print("\n Number : " + num);
		// Display calculated result
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitSwap = new BitSwap();
		// (37)    100101
		//         10 01 01
		//         01 10 10
		// Result  (011010) (26)
		task.swapBits(37);
		// (216)    11011000
		//          11 01 10 00
		// Swap     11 10 01 00
		// Result  (11100100) (228)
		task.swapBits(216);
		// (42)     101010
		//          10 10 10
		// Swap     01 01 01
		// Result  (010101) (21)
		task.swapBits(42);
	}
}

Output

 Number : 37
 Result : 26
 Number : 216
 Result : 228
 Number : 42
 Result : 21
/*
  Swift 4 program
  Swap all adjacent bits in given number
*/
class BitSwap
{
	// Swap the pair of adjacent bits
	func swapBits(_ num: Int)
	{
		// 0xAAAAAAAA (10101010101010101010101010101010) (2863311530)
		// 0x55555555 (01010101010101010101010101010101) (1431655765)
		// (num & 0xAAAAAAAA) Get the set bits in Even position of num
		// (num & 0x55555555) Get the set bits in Odd position of num
		// ((Even active bits) >> 1) shift 1 bits right side
		// ((Odd active bits) << 1)  shift 1 bits left side
		let result: Int = (((num & 0xAAAAAAAA) >> 1) | ((num & 0x55555555) << 1));
		// Display given number
		print("\n Number : ", num, terminator: "");
		// Display calculated result
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let task: BitSwap = BitSwap();
	// (37)    100101
	//         10 01 01
	//         01 10 10
	// Result  (011010) (26)
	task.swapBits(37);
	// (216)    11011000
	//          11 01 10 00
	// Swap     11 10 01 00
	// Result  (11100100) (228)
	task.swapBits(216);
	// (42)     101010
	//          10 10 10
	// Swap     01 01 01
	// Result  (010101) (21)
	task.swapBits(42);
}
main();

Output

 Number :  37
 Result :  26
 Number :  216
 Result :  228
 Number :  42
 Result :  21


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