Find absolute difference between set and unset bit of a number

Here given code implementation process.

/*
    Java Program
    Find absolute difference between set and unset bit of a number
*/
public class BinaryBits
{
	public int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void bitDifference(int num)
	{
		int setBit = 0;
		int unSetBit = 0;
		int n = num;
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		int result = abseValue(setBit - unSetBit);
		// Display given number
		System.out.println(" Given number  : " + num);
		// Display calculated result
		System.out.println(" Bit different : " + result);
	}
	public static void main(String[] args)
	{
		BinaryBits task = new BinaryBits();
		// Test A
		// num = 536  binary (1000011000)
		//  1000011000
		//  -    --
		// set bit   =  3
		//
		//  1000011000
		//   ----  ---    
		// unset bit = 7
		// different  = (3 - 7) = -4  = abs(4)
		// -----------------------------------
		// result = 4 
		task.bitDifference(536);
		// Test B
		// num = 143  binary (10001111)
		//  10001111
		//  -   ----
		// set bit   =  5
		//
		//  10001111
		//   ---    
		// unset bit =  3
		// different  = (5 - 2) = 3
		// -----------------------------------
		// result = 3 
		task.bitDifference(143);
		// Test C
		// num = 231  binary (11100111)
		//  11100111
		//  ---  ---
		// set bit   =  6
		// 
		//  11100111
		//     --   
		// unset bit =  2
		// different  = (6 - 2) = 4
		// -----------------------------------
		// result = 4
		task.bitDifference(231);
	}
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
// C Program for
// Find absolute difference between set and unset bit of a number
#include <stdio.h>
int abseValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void bitDifference(int num)
{
	int setBit = 0;
	int unSetBit = 0;
	int n = num;
	while (n != 0)
	{
		if ((n & 1) == 1)
		{
			// Count the set bit
			setBit += 1;
		}
		else
		{
			// Count the unset bit
			unSetBit += 1;
		}
		// Shift n value by 1 to right
		n = (n >> 1);
	}
	// Calculate bit difference
	int result = abseValue(setBit - unSetBit);
	// Display given number
	printf(" Given number  : %d\n", num);
	// Display calculated result
	printf(" Bit different : %d\n", result);
}
int main(int argc, char
	const *argv[])
{
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	bitDifference(231);
	return 0;
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits
{
	public: int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void bitDifference(int num)
	{
		int setBit = 0;
		int unSetBit = 0;
		int n = num;
		while (n != 0)
		{
			if ((n &1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		int result = this->abseValue(setBit - unSetBit);
		// Display given number
		cout << " Given number  : " << num << endl;
		// Display calculated result
		cout << " Bit different : " << result << endl;
	}
};
int main()
{
	BinaryBits *task = new BinaryBits();
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	task->bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	task->bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	task->bitDifference(231);
	return 0;
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
// Include namespace system
using System;
/*
    Csharp Program
    Find absolute difference between set and unset bit of a number
*/
public class BinaryBits
{
	public int abseValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void bitDifference(int num)
	{
		int setBit = 0;
		int unSetBit = 0;
		int n = num;
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		int result = this.abseValue(setBit - unSetBit);
		// Display given number
		Console.WriteLine(" Given number  : " + num);
		// Display calculated result
		Console.WriteLine(" Bit different : " + result);
	}
	public static void Main(String[] args)
	{
		BinaryBits task = new BinaryBits();
		// Test A
		// num = 536  binary (1000011000)
		//  1000011000
		//  -    --
		// set bit   =  3
		//
		//  1000011000
		//   ----  ---    
		// unset bit = 7
		// different  = (3 - 7) = -4  = abs(4)
		// -----------------------------------
		// result = 4 
		task.bitDifference(536);
		// Test B
		// num = 143  binary (10001111)
		//  10001111
		//  -   ----
		// set bit   =  5
		//
		//  10001111
		//   ---    
		// unset bit =  3
		// different  = (5 - 2) = 3
		// -----------------------------------
		// result = 3 
		task.bitDifference(143);
		// Test C
		// num = 231  binary (11100111)
		//  11100111
		//  ---  ---
		// set bit   =  6
		// 
		//  11100111
		//     --   
		// unset bit =  2
		// different  = (6 - 2) = 4
		// -----------------------------------
		// result = 4
		task.bitDifference(231);
	}
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
package main
import "fmt"
/*
    Go Program
    Find absolute difference between set and unset bit of a number
*/
type BinaryBits struct {}
func getBinaryBits() * BinaryBits {
	var me *BinaryBits = &BinaryBits {}
	return me
}
func(this BinaryBits) abseValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
func(this BinaryBits) bitDifference(num int) {
	var setBit int = 0
	var unSetBit int = 0
	var n int = num
	for (n != 0) {
		if (n & 1) == 1 {
			// Count the set bit
			setBit += 1
		} else {
			// Count the unset bit
			unSetBit += 1
		}
		// Shift n value by 1 to right
		n = (n >> 1)
	}
	// Calculate bit difference
	var result int = this.abseValue(setBit - unSetBit)
	// Display given number
	fmt.Println(" Given number  : ", num)
	// Display calculated result
	fmt.Println(" Bit different : ", result)
}
func main() {
	var task * BinaryBits = getBinaryBits()
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	task.bitDifference(536)
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	task.bitDifference(143)
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	task.bitDifference(231)
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
<?php
/*
    Php Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits
{
	public	function abseValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function bitDifference($num)
	{
		$setBit = 0;
		$unSetBit = 0;
		$n = $num;
		while ($n != 0)
		{
			if (($n & 1) == 1)
			{
				// Count the set bit
				$setBit += 1;
			}
			else
			{
				// Count the unset bit
				$unSetBit += 1;
			}
			// Shift n value by 1 to right
			$n = ($n >> 1);
		}
		// Calculate bit difference
		$result = $this->abseValue($setBit - $unSetBit);
		// Display given number
		echo(" Given number  : ".$num.
			"\n");
		// Display calculated result
		echo(" Bit different : ".$result.
			"\n");
	}
}

function main()
{
	$task = new BinaryBits();
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	$task->bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	$task->bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	$task->bitDifference(231);
}
main();

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
/*
    Node JS Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits
{
	abseValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	bitDifference(num)
	{
		var setBit = 0;
		var unSetBit = 0;
		var n = num;
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		var result = this.abseValue(setBit - unSetBit);
		// Display given number
		console.log(" Given number  : " + num);
		// Display calculated result
		console.log(" Bit different : " + result);
	}
}

function main()
{
	var task = new BinaryBits();
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	task.bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	task.bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	task.bitDifference(231);
}
main();

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
#    Python 3 Program
#    Find absolute difference between set and unset bit of a number
class BinaryBits :
	def abseValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def bitDifference(self, num) :
		setBit = 0
		unSetBit = 0
		n = num
		while (n != 0) :
			if ((n & 1) == 1) :
				#  Count the set bit
				setBit += 1
			else :
				#  Count the unset bit
				unSetBit += 1
			
			#  Shift n value by 1 to right
			n = (n >> 1)
		
		#  Calculate bit difference
		result = self.abseValue(setBit - unSetBit)
		#  Display given number
		print(" Given number  : ", num)
		#  Display calculated result
		print(" Bit different : ", result)
	

def main() :
	task = BinaryBits()
	#  Test A
	#  num = 536  binary (1000011000)
	#   1000011000
	#   -    --
	#  set bit   =  3
	#   1000011000
	#    ----  ---    
	#  unset bit = 7
	#  different  = (3 - 7) = -4  = abs(4)
	#  -----------------------------------
	#  result = 4 
	task.bitDifference(536)
	#  Test B
	#  num = 143  binary (10001111)
	#   10001111
	#   -   ----
	#  set bit   =  5
	#   10001111
	#    ---    
	#  unset bit =  3
	#  different  = (5 - 2) = 3
	#  -----------------------------------
	#  result = 3 
	task.bitDifference(143)
	#  Test C
	#  num = 231  binary (11100111)
	#   11100111
	#   ---  ---
	#  set bit   =  6
	#   11100111
	#      --   
	#  unset bit =  2
	#  different  = (6 - 2) = 4
	#  -----------------------------------
	#  result = 4
	task.bitDifference(231)

if __name__ == "__main__": main()

Output

 Given number  :  536
 Bit different :  4
 Given number  :  143
 Bit different :  2
 Given number  :  231
 Bit different :  4
#    Ruby Program
#    Find absolute difference between set and unset bit of a number
class BinaryBits 
	def abseValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def bitDifference(num) 
		setBit = 0
		unSetBit = 0
		n = num
		while (n != 0) 
			if ((n & 1) == 1) 
				#  Count the set bit
				setBit += 1
			else
 
				#  Count the unset bit
				unSetBit += 1
			end

			#  Shift n value by 1 to right
			n = (n >> 1)
		end

		#  Calculate bit difference
		result = self.abseValue(setBit - unSetBit)
		#  Display given number
		print(" Given number  : ", num, "\n")
		#  Display calculated result
		print(" Bit different : ", result, "\n")
	end

end

def main() 
	task = BinaryBits.new()
	#  Test A
	#  num = 536  binary (1000011000)
	#   1000011000
	#   -    --
	#  set bit   =  3
	#   1000011000
	#    ----  ---    
	#  unset bit = 7
	#  different  = (3 - 7) = -4  = abs(4)
	#  -----------------------------------
	#  result = 4 
	task.bitDifference(536)
	#  Test B
	#  num = 143  binary (10001111)
	#   10001111
	#   -   ----
	#  set bit   =  5
	#   10001111
	#    ---    
	#  unset bit =  3
	#  different  = (5 - 2) = 3
	#  -----------------------------------
	#  result = 3 
	task.bitDifference(143)
	#  Test C
	#  num = 231  binary (11100111)
	#   11100111
	#   ---  ---
	#  set bit   =  6
	#   11100111
	#      --   
	#  unset bit =  2
	#  different  = (6 - 2) = 4
	#  -----------------------------------
	#  result = 4
	task.bitDifference(231)
end

main()

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
/*
    Scala Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits()
{
	def abseValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def bitDifference(num: Int): Unit = {
		var setBit: Int = 0;
		var unSetBit: Int = 0;
		var n: Int = num;
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		var result: Int = abseValue(setBit - unSetBit);
		// Display given number
		println(" Given number  : " + num);
		// Display calculated result
		println(" Bit different : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BinaryBits = new BinaryBits();
		// Test A
		// num = 536  binary (1000011000)
		//  1000011000
		//  -    --
		// set bit   =  3
		//
		//  1000011000
		//   ----  ---    
		// unset bit = 7
		// different  = (3 - 7) = -4  = abs(4)
		// -----------------------------------
		// result = 4 
		task.bitDifference(536);
		// Test B
		// num = 143  binary (10001111)
		//  10001111
		//  -   ----
		// set bit   =  5
		//
		//  10001111
		//   ---    
		// unset bit =  3
		// different  = (5 - 2) = 3
		// -----------------------------------
		// result = 3 
		task.bitDifference(143);
		// Test C
		// num = 231  binary (11100111)
		//  11100111
		//  ---  ---
		// set bit   =  6
		// 
		//  11100111
		//     --   
		// unset bit =  2
		// different  = (6 - 2) = 4
		// -----------------------------------
		// result = 4
		task.bitDifference(231);
	}
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4
/*
    Swift 4 Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits
{
	func abseValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func bitDifference(_ num: Int)
	{
		var setBit: Int = 0;
		var unSetBit: Int = 0;
		var n: Int = num;
		while (n  != 0)
		{
			if ((n & 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n >> 1);
		}
		// Calculate bit difference
		let result: Int = self.abseValue(setBit - unSetBit);
		// Display given number
		print(" Given number  : ", num);
		// Display calculated result
		print(" Bit different : ", result);
	}
}
func main()
{
	let task: BinaryBits = BinaryBits();
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	task.bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	task.bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	task.bitDifference(231);
}
main();

Output

 Given number  :  536
 Bit different :  4
 Given number  :  143
 Bit different :  2
 Given number  :  231
 Bit different :  4
/*
    Kotlin Program
    Find absolute difference between set and unset bit of a number
*/
class BinaryBits
{
	fun abseValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun bitDifference(num: Int): Unit
	{
		var setBit: Int = 0;
		var unSetBit: Int = 0;
		var n: Int = num;
		while (n != 0)
		{
			if ((n and 1) == 1)
			{
				// Count the set bit
				setBit += 1;
			}
			else
			{
				// Count the unset bit
				unSetBit += 1;
			}
			// Shift n value by 1 to right
			n = (n shr 1);
		}
		// Calculate bit difference
		val result: Int = this.abseValue(setBit - unSetBit);
		// Display given number
		println(" Given number  : " + num);
		// Display calculated result
		println(" Bit different : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BinaryBits = BinaryBits();
	// Test A
	// num = 536  binary (1000011000)
	//  1000011000
	//  -    --
	// set bit   =  3
	//
	//  1000011000
	//   ----  ---    
	// unset bit = 7
	// different  = (3 - 7) = -4  = abs(4)
	// -----------------------------------
	// result = 4 
	task.bitDifference(536);
	// Test B
	// num = 143  binary (10001111)
	//  10001111
	//  -   ----
	// set bit   =  5
	//
	//  10001111
	//   ---    
	// unset bit =  3
	// different  = (5 - 2) = 3
	// -----------------------------------
	// result = 3 
	task.bitDifference(143);
	// Test C
	// num = 231  binary (11100111)
	//  11100111
	//  ---  ---
	// set bit   =  6
	// 
	//  11100111
	//     --   
	// unset bit =  2
	// different  = (6 - 2) = 4
	// -----------------------------------
	// result = 4
	task.bitDifference(231);
}

Output

 Given number  : 536
 Bit different : 4
 Given number  : 143
 Bit different : 2
 Given number  : 231
 Bit different : 4


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