Skip to main content

Bit difference between two numbers

Here given code implementation process.

// C program 
// Bit difference between two numbers
#include <stdio.h>

// Find bit difference of given two numbers
void bit_difference(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);
	}
	printf("\n Bit difference : %d", (count *2));
}
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) 
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (2 2 0 0 2 0)
	// difference = 6
	bit_difference(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0) 
	//            ↓ ↓ ↓ ↓ 
	//           (2 0 0 0 )
	// Difference = 2
	bit_difference(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓ 
	//           (0 0 0 )
	// Difference  = 0
	bit_difference(a, b);
	return 0;
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
/*
  Java program
  Bit difference between two numbers
*/
public class Difference
{
	// Find bit difference of given two numbers
	public void bitDifference(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);
		}
		System.out.print("\n Bit difference : " + (count * 2) + "");
	}
	public static void main(String[] args)
	{
		Difference task = new Difference();
		int a = 43;
		int b = 25;
		// a = 43    (1 0 1 0 1 1)
		// b = 25    (0 1 1 0 0 1) 
		//            ↓ ↓ ↓ ↓ ↓ ↓
		//           (2 2 0 0 2 0)
		// Difference = 6
		task.bitDifference(a, b);
		// Case 2
		a = 12;
		b = 4;
		// a = 12    (1 1 0 0)
		// b = 4     (0 1 0 0) 
		//            ↓ ↓ ↓ ↓ 
		//           (2 0 0 0 )
		// Difference = 2
		task.bitDifference(a, b);
		// Case 3
		a = 7;
		b = 7;
		// a = 7     (1 1 1 )
		// b = 7     (1 1 1 )
		//            ↓ ↓ ↓ 
		//           (0 0 0 )
		// Difference  = 0
		task.bitDifference(a, b);
	}
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
// Include header file
#include <iostream>
using namespace std;

/*
  C++ program
  Bit difference between two numbers
*/

class Difference
{
	public:
		// Find bit difference of given two numbers
		void bitDifference(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);
			}
			cout << "\n Bit difference : " << (count *2) << "";
		}
};
int main()
{
	Difference task = Difference();
	int a = 43;
	int b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (2 2 0 0 2 0)
	// Difference = 6
	task.bitDifference(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (2 0 0 0 )
	// Difference = 2
	task.bitDifference(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Difference  = 0
	task.bitDifference(a, b);
	return 0;
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
// Include namespace system
using System;
/*
  C# program
  Bit difference between two numbers
*/
public class Difference
{
	// Find bit difference of given two numbers
	public void bitDifference(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);
		}
		Console.Write("\n Bit difference : " + (count * 2) + "");
	}
	public static void Main(String[] args)
	{
		Difference task = new Difference();
		int a = 43;
		int b = 25;
		// a = 43    (1 0 1 0 1 1)
		// b = 25    (0 1 1 0 0 1)
		//            ↓ ↓ ↓ ↓ ↓ ↓
		//           (2 2 0 0 2 0)
		// Difference = 6
		task.bitDifference(a, b);
		// Case 2
		a = 12;
		b = 4;
		// a = 12    (1 1 0 0)
		// b = 4     (0 1 0 0)
		//            ↓ ↓ ↓ ↓
		//           (2 0 0 0 )
		// Difference = 2
		task.bitDifference(a, b);
		// Case 3
		a = 7;
		b = 7;
		// a = 7     (1 1 1 )
		// b = 7     (1 1 1 )
		//            ↓ ↓ ↓
		//           (0 0 0 )
		// Difference  = 0
		task.bitDifference(a, b);
	}
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
<?php
/*
  Php program
  Bit difference between two numbers
*/
class Difference
{
	// Find bit difference of given two numbers
	public	function bitDifference($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);
		}
		echo "\n Bit difference : ". ($count * 2) ."";
	}
}

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

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
/*
  Node Js program
  Bit difference between two numbers
*/
class Difference
{
	// Find bit difference of given two numbers
	bitDifference(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);
		}
		process.stdout.write("\n Bit difference : " + (count * 2) + "");
	}
}

function main()
{
	var task = new Difference();
	var a = 43;
	var b = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (2 2 0 0 2 0)
	// Difference = 6
	task.bitDifference(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (2 0 0 0 )
	// Difference = 2
	task.bitDifference(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Difference  = 0
	task.bitDifference(a, b);
}
main();

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
#   Python 3 program
#   Bit difference between two numbers

class Difference :
	#  Find bit difference of given two numbers
	def bitDifference(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)
		
		print("\n Bit difference : ", (count * 2) ,"", end = "")
	

def main() :
	task = Difference()
	a = 43
	b = 25
	#  a = 43    (1 0 1 0 1 1)
	#  b = 25    (0 1 1 0 0 1) 
	#             ↓ ↓ ↓ ↓ ↓ ↓
	#            (2 2 0 0 2 0)
	#  Difference = 6
	task.bitDifference(a, b)
	#  Case 2
	a = 12
	b = 4
	#  a = 12    (1 1 0 0)
	#  b = 4     (0 1 0 0) 
	#             ↓ ↓ ↓ ↓ 
	#            (2 0 0 0 )
	#  Difference = 2
	task.bitDifference(a, b)
	#  Case 3
	a = 7
	b = 7
	#  a = 7     (1 1 1 )
	#  b = 7     (1 1 1 )
	#             ↓ ↓ ↓ 
	#            (0 0 0 )
	#  Difference  = 0
	task.bitDifference(a, b)

if __name__ == "__main__": main()

Output

 Given number ( a :  43 , b :  25 )
 Bit difference :  6
 Given number ( a :  12 , b :  4 )
 Bit difference :  2
 Given number ( a :  7 , b :  7 )
 Bit difference :  0
#   Ruby program
#   Bit difference between two numbers

class Difference 
	#  Find bit difference of given two numbers
	def bitDifference(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

		print("\n Bit difference : ", (count * 2) ,"")
	end

end

def main() 
	task = Difference.new()
	a = 43
	b = 25
	#  a = 43    (1 0 1 0 1 1)
	#  b = 25    (0 1 1 0 0 1) 
	#             ↓ ↓ ↓ ↓ ↓ ↓
	#            (2 2 0 0 2 0)
	#  Difference = 6
	task.bitDifference(a, b)
	#  Case 2
	a = 12
	b = 4
	#  a = 12    (1 1 0 0)
	#  b = 4     (0 1 0 0) 
	#             ↓ ↓ ↓ ↓ 
	#            (2 0 0 0 )
	#  Difference = 2
	task.bitDifference(a, b)
	#  Case 3
	a = 7
	b = 7
	#  a = 7     (1 1 1 )
	#  b = 7     (1 1 1 )
	#             ↓ ↓ ↓ 
	#            (0 0 0 )
	#  Difference  = 0
	task.bitDifference(a, b)
end

main()

Output

 Given number ( a : 43, b : 25) 
 Bit difference : 6
 Given number ( a : 12, b : 4) 
 Bit difference : 2
 Given number ( a : 7, b : 7) 
 Bit difference : 0
/*
  Scala program
  Bit difference between two numbers
*/
class Difference
{
	// Find bit difference of given two numbers
	def bitDifference(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);
		}
		print("\n Bit difference : " + (count * 2) + "");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Difference = new Difference();
		var a: Int = 43;
		var b: Int = 25;
		// a = 43    (1 0 1 0 1 1)
		// b = 25    (0 1 1 0 0 1)
		//            ↓ ↓ ↓ ↓ ↓ ↓
		//           (2 2 0 0 2 0)
		// Difference = 6
		task.bitDifference(a, b);
		// Case 2
		a = 12;
		b = 4;
		// a = 12    (1 1 0 0)
		// b = 4     (0 1 0 0)
		//            ↓ ↓ ↓ ↓
		//           (2 0 0 0 )
		// Difference = 2
		task.bitDifference(a, b);
		// Case 3
		a = 7;
		b = 7;
		// a = 7     (1 1 1 )
		// b = 7     (1 1 1 )
		//            ↓ ↓ ↓
		//           (0 0 0 )
		// Difference  = 0
		task.bitDifference(a, b);
	}
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 0
/*
  Swift 4 program
  Bit difference between two numbers
*/
class Difference
{
	// Find bit difference of given two numbers
	func bitDifference(_ 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);
		}
		print("\n Bit difference : ", (count * 2) ,"", terminator: "");
	}
}
func main()
{
	let task: Difference = Difference();
	var a: Int = 43;
	var b: Int = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (2 2 0 0 2 0)
	// Difference = 6
	task.bitDifference(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (2 0 0 0 )
	// Difference = 2
	task.bitDifference(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Difference  = 0
	task.bitDifference(a, b);
}
main();

Output

 Given number ( a :  43 , b :  25 )
 Bit difference :  6
 Given number ( a :  12 , b :  4 )
 Bit difference :  2
 Given number ( a :  7 , b :  7 )
 Bit difference :  0
/*
  Kotlin program
  Bit difference between two numbers
*/
class Difference
{
	// Find bit difference of given two numbers
	fun bitDifference(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);
		}
		print("\n Bit difference : " + (count * 2) + "");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Difference = Difference();
	var a: Int = 43;
	var b: Int = 25;
	// a = 43    (1 0 1 0 1 1)
	// b = 25    (0 1 1 0 0 1)
	//            ↓ ↓ ↓ ↓ ↓ ↓
	//           (2 2 0 0 2 0)
	// Difference = 6
	task.bitDifference(a, b);
	// Case 2
	a = 12;
	b = 4;
	// a = 12    (1 1 0 0)
	// b = 4     (0 1 0 0)
	//            ↓ ↓ ↓ ↓
	//           (2 0 0 0 )
	// Difference = 2
	task.bitDifference(a, b);
	// Case 3
	a = 7;
	b = 7;
	// a = 7     (1 1 1 )
	// b = 7     (1 1 1 )
	//            ↓ ↓ ↓
	//           (0 0 0 )
	// Difference  = 0
	task.bitDifference(a, b);
}

Output

 Given number ( a : 43, b : 25)
 Bit difference : 6
 Given number ( a : 12, b : 4)
 Bit difference : 2
 Given number ( a : 7, b : 7)
 Bit difference : 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