Skip to main content

Swap two nibbles in a byte

Here given code implementation process.

// C program 
// Swap two nibbles in a byte
#include <stdio.h>

void swapNibbles(int num)
{
    // (  num & 15 ) Get initial 4 bit set bits in number
    // (( num & 15) << 4) this is moving initial 4 bits in left side [A]
    // (( num & 240)) Get next 4 bit set bits in number
    // (( num & 240) >> 4 ) moving next 4 bits in right side [B]
    // A or B return swap version of 4 bits
    int result = ((num & 15) << 4) | ((num & 240) >> 4 ); 

    printf("\n Number : %d",num);
    printf("\n Result : %d",result);
}
int main(int argc, char const *argv[])
{
    
    // (175)   10101111
    // 4 (bit) 1010 1111
    //            ↘ ↙
    //         1111 1010
    // Result  (11111010) (250)
    swapNibbles(175);
    // (225)   11100001
    // 4 (bit) 1110 0001
    //            ↘ ↙
    //         0001 1110
    // Result  (00011110) (30)
    swapNibbles(225);
    // (2)     00000010
    // 4 (bit) 0000 0010
    //            ↘ ↙
    //         0010 0000
    // Result  (00100000) (32)
    swapNibbles(2);

    return 0;
}

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
/*
  Java program
  Swap two nibbles in a byte 
*/
public class BitSwap
{
	// Interchange 4 bits of a number
	public void swapNibbles(int num)
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		int result = ((num & 15) << 4) | ((num & 240) >> 4);
		System.out.print("\n Number : " + num);
		System.out.print("\n Result : " + result);
	}
	public static void main(String[] args)
	{
		BitSwap task = new BitSwap();
		// (175)   10101111
		// 4 (bit) 1010 1111
		//            ↘ ↙
		//         1111 1010
		// Result  (11111010) (250)
		task.swapNibbles(175);
		// (225)   11100001
		// 4 (bit) 1110 0001
		//            ↘ ↙
		//         0001 1110
		// Result  (00011110) (30)
		task.swapNibbles(225);
		// (2)     00000010
		// 4 (bit) 0000 0010
		//            ↘ ↙
		//         0010 0000
		// Result  (00100000) (32)
		task.swapNibbles(2);
	}
}

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
// Include header file
#include <iostream>
using namespace std;
/*
  C++ program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	public:
		// Interchange 4 bits of a number
		void swapNibbles(int num)
		{
			// (  num &15 ) Get initial 4 bit set bits in number
			// (( num &15) << 4) this is moving initial 4 bits in left side [A]
			// (( num &240)) Get next 4 bit set bits in number
			// (( num &240) >> 4 ) moving next 4 bits in right side [B]
			// A or B return swap version of 4 bits
			int result = ((num &15) << 4) | ((num &240) >> 4);
			cout << "\n Number : " << num;
			cout << "\n Result : " << result;
		}
};
int main()
{
	BitSwap task = BitSwap();
	// (175)   10101111
	// 4 (bit) 1010 1111
	//            ↘ ↙
	//         1111 1010
	// Result  (11111010) (250)
	task.swapNibbles(175);
	// (225)   11100001
	// 4 (bit) 1110 0001
	//            ↘ ↙
	//         0001 1110
	// Result  (00011110) (30)
	task.swapNibbles(225);
	// (2)     00000010
	// 4 (bit) 0000 0010
	//            ↘ ↙
	//         0010 0000
	// Result  (00100000) (32)
	task.swapNibbles(2);
	return 0;
}

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
// Include namespace system
using System;
/*
  C# program
  Swap two nibbles in a byte 
*/
public class BitSwap
{
	// Interchange 4 bits of a number
	public void swapNibbles(int num)
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		int result = ((num & 15) << 4) | ((num & 240) >> 4);
		Console.Write("\n Number : " + num);
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		BitSwap task = new BitSwap();
		// (175)   10101111
		// 4 (bit) 1010 1111
		//            ↘ ↙
		//         1111 1010
		// Result  (11111010) (250)
		task.swapNibbles(175);
		// (225)   11100001
		// 4 (bit) 1110 0001
		//            ↘ ↙
		//         0001 1110
		// Result  (00011110) (30)
		task.swapNibbles(225);
		// (2)     00000010
		// 4 (bit) 0000 0010
		//            ↘ ↙
		//         0010 0000
		// Result  (00100000) (32)
		task.swapNibbles(2);
	}
}

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
<?php
/*
  Php program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	// Interchange 4 bits of a number
	public	function swapNibbles($num)
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		$result = (($num & 15) << 4) | (($num & 240) >> 4);
		echo "\n Number : ". $num;
		echo "\n Result : ". $result;
	}
}

function main()
{
	$task = new BitSwap();
	// (175)   10101111
	// 4 (bit) 1010 1111
	//            ↘ ↙
	//         1111 1010
	// Result  (11111010) (250)
	$task->swapNibbles(175);
	// (225)   11100001
	// 4 (bit) 1110 0001
	//            ↘ ↙
	//         0001 1110
	// Result  (00011110) (30)
	$task->swapNibbles(225);
	// (2)     00000010
	// 4 (bit) 0000 0010
	//            ↘ ↙
	//         0010 0000
	// Result  (00100000) (32)
	$task->swapNibbles(2);
}
main();

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
/*
  Node Js program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	// Interchange 4 bits of a number
	swapNibbles(num)
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		var result = ((num & 15) << 4) | ((num & 240) >> 4);
		process.stdout.write("\n Number : " + num);
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var task = new BitSwap();
	// (175)   10101111
	// 4 (bit) 1010 1111
	//            ↘ ↙
	//         1111 1010
	// Result  (11111010) (250)
	task.swapNibbles(175);
	// (225)   11100001
	// 4 (bit) 1110 0001
	//            ↘ ↙
	//         0001 1110
	// Result  (00011110) (30)
	task.swapNibbles(225);
	// (2)     00000010
	// 4 (bit) 0000 0010
	//            ↘ ↙
	//         0010 0000
	// Result  (00100000) (32)
	task.swapNibbles(2);
}
main();

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
#   Python 3 program
#   Swap two nibbles in a byte 

class BitSwap :
	#  Interchange 4 bits of a number
	def swapNibbles(self, num) :
		#  (  num & 15 ) Get initial 4 bit set bits in number
		#  (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		#  (( num & 240)) Get next 4 bit set bits in number
		#  (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		#  A or B return swap version of 4 bits
		result = ((num & 15) << 4) | ((num & 240) >> 4)
		print("\n Number : ", num, end = "")
		print("\n Result : ", result, end = "")
	

def main() :
	task = BitSwap()
	#  (175)   10101111
	#  4 (bit) 1010 1111
	#             ↘ ↙
	#          1111 1010
	#  Result  (11111010) (250)
	task.swapNibbles(175)
	#  (225)   11100001
	#  4 (bit) 1110 0001
	#             ↘ ↙
	#          0001 1110
	#  Result  (00011110) (30)
	task.swapNibbles(225)
	#  (2)     00000010
	#  4 (bit) 0000 0010
	#             ↘ ↙
	#          0010 0000
	#  Result  (00100000) (32)
	task.swapNibbles(2)

if __name__ == "__main__": main()

Output

 Number :  175
 Result :  250
 Number :  225
 Result :  30
 Number :  2
 Result :  32
#   Ruby program
#   Swap two nibbles in a byte 

class BitSwap 
	#  Interchange 4 bits of a number
	def swapNibbles(num) 
		#  (  num & 15 ) Get initial 4 bit set bits in number
		#  (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		#  (( num & 240)) Get next 4 bit set bits in number
		#  (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		#  A or B return swap version of 4 bits
		result = ((num & 15) << 4) | ((num & 240) >> 4)
		print("\n Number : ", num)
		print("\n Result : ", result)
	end

end

def main() 
	task = BitSwap.new()
	#  (175)   10101111
	#  4 (bit) 1010 1111
	#             ↘ ↙
	#          1111 1010
	#  Result  (11111010) (250)
	task.swapNibbles(175)
	#  (225)   11100001
	#  4 (bit) 1110 0001
	#             ↘ ↙
	#          0001 1110
	#  Result  (00011110) (30)
	task.swapNibbles(225)
	#  (2)     00000010
	#  4 (bit) 0000 0010
	#             ↘ ↙
	#          0010 0000
	#  Result  (00100000) (32)
	task.swapNibbles(2)
end

main()

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
/*
  Scala program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	// Interchange 4 bits of a number
	def swapNibbles(num: Int): Unit = {
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		var result: Int = ((num & 15) << 4) | ((num & 240) >> 4);
		print("\n Number : " + num);
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitSwap = new BitSwap();
		// (175)   10101111
		// 4 (bit) 1010 1111
		//            ↘ ↙
		//         1111 1010
		// Result  (11111010) (250)
		task.swapNibbles(175);
		// (225)   11100001
		// 4 (bit) 1110 0001
		//            ↘ ↙
		//         0001 1110
		// Result  (00011110) (30)
		task.swapNibbles(225);
		// (2)     00000010
		// 4 (bit) 0000 0010
		//            ↘ ↙
		//         0010 0000
		// Result  (00100000) (32)
		task.swapNibbles(2);
	}
}

Output

 Number : 175
 Result : 250
 Number : 225
 Result : 30
 Number : 2
 Result : 32
/*
  Swift 4 program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	// Interchange 4 bits of a number
	func swapNibbles(_ num: Int)
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		let result: Int = ((num & 15) << 4) | ((num & 240) >> 4);
		print("\n Number : ", num, terminator: "");
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let task: BitSwap = BitSwap();
	// (175)   10101111
	// 4 (bit) 1010 1111
	//            ↘ ↙
	//         1111 1010
	// Result  (11111010) (250)
	task.swapNibbles(175);
	// (225)   11100001
	// 4 (bit) 1110 0001
	//            ↘ ↙
	//         0001 1110
	// Result  (00011110) (30)
	task.swapNibbles(225);
	// (2)     00000010
	// 4 (bit) 0000 0010
	//            ↘ ↙
	//         0010 0000
	// Result  (00100000) (32)
	task.swapNibbles(2);
}
main();

Output

 Number :  175
 Result :  250
 Number :  225
 Result :  30
 Number :  2
 Result :  32
/*
  Kotlin program
  Swap two nibbles in a byte 
*/
class BitSwap
{
	// Interchange 4 bits of a number
	fun swapNibbles(num: Int): Unit
	{
		// (  num & 15 ) Get initial 4 bit set bits in number
		// (( num & 15) << 4) this is moving initial 4 bits in left side [A]
		// (( num & 240)) Get next 4 bit set bits in number
		// (( num & 240) >> 4 ) moving next 4 bits in right side [B]
		// A or B return swap version of 4 bits
		var result: Int = ((num and 15) shl 4) or((num and 240) shr 4);
		print("\n Number : " + num);
		print("\n Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitSwap = BitSwap();
	// (175)   10101111
	// 4 (bit) 1010 1111
	//            ↘ ↙
	//         1111 1010
	// Result  (11111010) (250)
	task.swapNibbles(175);
	// (225)   11100001
	// 4 (bit) 1110 0001
	//            ↘ ↙
	//         0001 1110
	// Result  (00011110) (30)
	task.swapNibbles(225);
	// (2)     00000010
	// 4 (bit) 0000 0010
	//            ↘ ↙
	//         0010 0000
	// Result  (00100000) (32)
	task.swapNibbles(2);
}

Output

 Number :  175
 Result :  250
 Number :  225
 Result :  30
 Number :  2
 Result :  32




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.

New Comment