Skip to main content

Count number of bits to be flipped to convert A to B

Here given code implementation process.

// C program 
// Count number of bits to be flipped to convert A to B
#include <stdio.h>

// Calculate how many bit changes are required to make equal numbers
void flippedCount(int a, int b)
{
	// Display given number
	printf("\n Given number ( a : %d, b : %d) ", a, b);
	int num = (a ^ b);
	int count = 0;
	// Count number of active bits
	while (num > 0)
	{
		count++;
		num = num & (num - 1);
	}
	// Display calculated result 
	printf("\n Output : %d", (count));
}
int main(int argc, char
	const *argv[])
{
	int a = 43;
	int b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1) 
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	flippedCount(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0) 
	//            ↓ ↓ ↓ ↓ 
	//           (1 0 0 0 )
	// Bit changes =  1
	flippedCount(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓ 
	//           (0 0 0 )
	// Bit changes =  0
	flippedCount(a, b);
	return 0;
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
/*
  Java program
  Count number of bits to be flipped to convert A to B
*/
public class BitManipulation
{
    // Calculate how many bit changes are required to make equal numbers
    public void flippedCount(int a, int b)
    {
        // Display given number
        System.out.print("\n Given number ( a : " + a + ", b : " + b + ") ");
        int num = (a ^ b);
        int count = 0;
        // Count number of active bits
        while (num > 0)
        {
            count++;
            num = num & (num - 1);
        }
        // Display calculated result 
        System.out.print("\n Output : " + (count) );
    }

    public static void main(String[] args)
    {
        BitManipulation task = new BitManipulation();
        int a = 43;
        int b = 25;
        // a = 43    (1 0 1 0 1 1)
        // b = 25    (0 1 1 0 0 1) 
        //            ↓ ↓ ↓ ↓ ↓ ↓
        //           (1 1 0 0 1 0)
        // Bit changes = 3
        task.flippedCount(a, b);
        // Case 2
        a = 12;
        b = 4;
        // a = 12    (1 1 0 0)
        // b = 4     (0 1 0 0) 
        //            ↓ ↓ ↓ ↓ 
        //           (1 0 0 0 )
        // Bit changes =  1
        task.flippedCount(a, b);
        // Case 3
        a = 7;
        b = 7;
        // a = 7     (1 1 1 )
        // b = 7     (1 1 1 )
        //            ↓ ↓ ↓ 
        //           (0 0 0 )
        // Bit changes =  0
        task.flippedCount(a, b);
    }
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	public:
		// Calculate how many bit changes are required to make equal numbers
		void flippedCount(int a, int b)
		{
			// Display given number
			cout << "\n Given number ( a : " << a << ", b : " << b << ") ";
			int num = (a ^ b);
			int count = 0;
			// Count number of active bits
			while (num > 0)
			{
				count++;
				num = num &(num - 1);
			}
			// Display calculated result
			cout << "\n Output : " << (count);
		}
};
int main()
{
	BitManipulation task = BitManipulation();
	int a = 43;
	int b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	task.flippedCount(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (1 0 0 0 )
	// Bit changes =  1
	task.flippedCount(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Bit changes =  0
	task.flippedCount(a, b);
	return 0;
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
<?php
/*
  Php program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	public	function flippedCount($a, $b)
	{
		// Display given number
		echo "\n Given number ( a : ". $a .", b : ". $b .") ";
		$num = ($a ^ $b);
		$count = 0;
		// Count number of active bits
		while ($num > 0)
		{
			$count++;
			$num = $num & ($num - 1);
		}
		// Display calculated result
		echo "\n Output : ". ($count);
	}
}

function main()
{
	$task = new BitManipulation();
	$a = 43;
	$b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	$task->flippedCount($a, $b);
	// Case 2
	$a = 12;
	$b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (1 0 0 0 )
	// Bit changes =  1
	$task->flippedCount($a, $b);
	// Case 3
	$a = 7;
	$b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Bit changes =  0
	$task->flippedCount($a, $b);
}
main();

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
// Include namespace system
using System;
/*
  C# program
  Count number of bits to be flipped to convert A to B
*/
public class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	public void flippedCount(int a, int b)
	{
		// Display given number
		Console.Write("\n Given number ( a : " + a + ", b : " + b + ") ");
		int num = (a ^ b);
		int count = 0;
		// Count number of active bits
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Display calculated result
		Console.Write("\n Output : " + (count));
	}
	public static void Main(String[] args)
	{
		BitManipulation task = new BitManipulation();
		int a = 43;
		int b = 25;
		// a = 43    (1 0 1 0 1 1)
		// b = 25    (0 1 1 0 0 1)
		//            ↓ ↓ ↓ ↓ ↓ ↓
		//           (1 1 0 0 1 0)
		// Bit changes = 3
		task.flippedCount(a, b);
		// Case 2
		a = 12;
		b = 4;
		// a = 12    (1 1 0 0)
		// b = 4     (0 1 0 0)
		//            ↓ ↓ ↓ ↓
		//           (1 0 0 0 )
		// Bit changes =  1
		task.flippedCount(a, b);
		// Case 3
		a = 7;
		b = 7;
		// a = 7     (1 1 1 )
		// b = 7     (1 1 1 )
		//            ↓ ↓ ↓
		//           (0 0 0 )
		// Bit changes =  0
		task.flippedCount(a, b);
	}
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
/*
  Node Js program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	flippedCount(a, b)
	{
		// Display given number
		process.stdout.write("\n Given number ( a : " + a + ", b : " + b + ") ");
		var num = (a ^ b);
		var count = 0;
		// Count number of active bits
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Display calculated result
		process.stdout.write("\n Output : " + (count));
	}
}

function main()
{
	var task = new BitManipulation();
	var a = 43;
	var b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	task.flippedCount(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (1 0 0 0 )
	// Bit changes =  1
	task.flippedCount(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Bit changes =  0
	task.flippedCount(a, b);
}
main();

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
#   Python 3 program
#   Count number of bits to be flipped to convert A to B

class BitManipulation :
	#  Calculate how many bit changes are required to make equal numbers
	def flippedCount(self, a, b) :
		#  Display given number
		print("\n Given number ( a : ", a ,", b : ", b ,") ", end = "")
		num = (a ^ b)
		count = 0
		#  Count number of active bits
		while (num > 0) :
			count += 1
			num = num & (num - 1)
		
		#  Display calculated result 
		print("\n Output : ", (count), end = "")
	

def main() :
	task = BitManipulation()
	a = 43
	b = 25
	#  a = 43    (1 0 1 0 1 1)
	#  b = 25    (0 1 1 0 0 1) 
	#             ↓ ↓ ↓ ↓ ↓ ↓
	#            (1 1 0 0 1 0)
	#  Bit changes = 3
	task.flippedCount(a, b)
	#  Case 2
	a = 12
	b = 4
	#  a = 12    (1 1 0 0)
	#  b = 4     (0 1 0 0) 
	#             ↓ ↓ ↓ ↓ 
	#            (1 0 0 0 )
	#  Bit changes =  1
	task.flippedCount(a, b)
	#  Case 3
	a = 7
	b = 7
	#  a = 7     (1 1 1 )
	#  b = 7     (1 1 1 )
	#             ↓ ↓ ↓ 
	#            (0 0 0 )
	#  Bit changes =  0
	task.flippedCount(a, b)

if __name__ == "__main__": main()

Output

 Given number ( a :  43 , b :  25 )
 Output :  3
 Given number ( a :  12 , b :  4 )
 Output :  1
 Given number ( a :  7 , b :  7 )
 Output :  0
#   Ruby program
#   Count number of bits to be flipped to convert A to B

class BitManipulation 
	#  Calculate how many bit changes are required to make equal numbers
	def flippedCount(a, b) 
		#  Display given number
		print("\n Given number ( a : ", a ,", b : ", b ,") ")
		num = (a ^ b)
		count = 0
		#  Count number of active bits
		while (num > 0) 
			count += 1
			num = num & (num - 1)
		end

		#  Display calculated result 
		print("\n Output : ", (count))
	end

end

def main() 
	task = BitManipulation.new()
	a = 43
	b = 25
	#  a = 43    (1 0 1 0 1 1)
	#  b = 25    (0 1 1 0 0 1) 
	#             ↓ ↓ ↓ ↓ ↓ ↓
	#            (1 1 0 0 1 0)
	#  Bit changes = 3
	task.flippedCount(a, b)
	#  Case 2
	a = 12
	b = 4
	#  a = 12    (1 1 0 0)
	#  b = 4     (0 1 0 0) 
	#             ↓ ↓ ↓ ↓ 
	#            (1 0 0 0 )
	#  Bit changes =  1
	task.flippedCount(a, b)
	#  Case 3
	a = 7
	b = 7
	#  a = 7     (1 1 1 )
	#  b = 7     (1 1 1 )
	#             ↓ ↓ ↓ 
	#            (0 0 0 )
	#  Bit changes =  0
	task.flippedCount(a, b)
end

main()

Output

 Given number ( a : 43, b : 25) 
 Output : 3
 Given number ( a : 12, b : 4) 
 Output : 1
 Given number ( a : 7, b : 7) 
 Output : 0
/*
  Scala program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	def flippedCount(a: Int, b: Int): Unit = {
		// Display given number
		print("\n Given number ( a : " + a + ", b : " + b + ") ");
		var num: Int = (a ^ b);
		var count: Int = 0;
		// Count number of active bits
		while (num > 0)
		{
			count += 1;
			num = num & (num - 1);
		}
		// Display calculated result
		print("\n Output : " + (count));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitManipulation = new BitManipulation();
		var a: Int = 43;
		var b: Int = 25;
		// a = 43    (1 0 1 0 1 1)
		// b = 25    (0 1 1 0 0 1)
		//            ↓ ↓ ↓ ↓ ↓ ↓
		//           (1 1 0 0 1 0)
		// Bit changes = 3
		task.flippedCount(a, b);
		// Case 2
		a = 12;
		b = 4;
		// a = 12    (1 1 0 0)
		// b = 4     (0 1 0 0)
		//            ↓ ↓ ↓ ↓
		//           (1 0 0 0 )
		// Bit changes =  1
		task.flippedCount(a, b);
		// Case 3
		a = 7;
		b = 7;
		// a = 7     (1 1 1 )
		// b = 7     (1 1 1 )
		//            ↓ ↓ ↓
		//           (0 0 0 )
		// Bit changes =  0
		task.flippedCount(a, b);
	}
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0
/*
  Swift 4 program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	func flippedCount(_ a: Int, _ b: Int)
	{
		// Display given number
		print("\n Given number ( a : ", a ,", b : ", b ,") ", terminator: "");
		var num: Int = (a ^ b);
		var count: Int = 0;
		// Count number of active bits
		while (num > 0)
		{
			count += 1;
			num = num & (num - 1);
		}
		// Display calculated result
		print("\n Output : ", (count), terminator: "");
	}
}
func main()
{
	let task: BitManipulation = BitManipulation();
	var a: Int = 43;
	var b: Int = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	task.flippedCount(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (1 0 0 0 )
	// Bit changes =  1
	task.flippedCount(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Bit changes =  0
	task.flippedCount(a, b);
}
main();

Output

 Given number ( a :  43 , b :  25 )
 Output :  3
 Given number ( a :  12 , b :  4 )
 Output :  1
 Given number ( a :  7 , b :  7 )
 Output :  0
/*
  Kotlin program
  Count number of bits to be flipped to convert A to B
*/
class BitManipulation
{
	// Calculate how many bit changes are required to make equal numbers
	fun flippedCount(a: Int, b: Int): Unit
	{
		// Display given number
		print("\n Given number ( a : " + a + ", b : " + b + ") ");
		var num: Int = (a xor b);
		var count: Int = 0;
		// Count number of active bits
		while (num > 0)
		{
			count += 1;
			num = num and(num - 1);
		}
		// Display calculated result
		print("\n Output : " + (count));
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitManipulation = BitManipulation();
	var a: Int = 43;
	var b: Int = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (1 1 0 0 1 0)
	// Bit changes = 3
	task.flippedCount(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (1 0 0 0 )
	// Bit changes =  1
	task.flippedCount(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Bit changes =  0
	task.flippedCount(a, b);
}

Output

 Given number ( a : 43, b : 25)
 Output : 3
 Given number ( a : 12, b : 4)
 Output : 1
 Given number ( a : 7, b : 7)
 Output : 0




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