Skip to main content

Check if a number has same number of set and unset bits

Here given code implementation process.

// C Program 
// Check if a number has same number of set and unset bits
#include <stdio.h>

// Check whether the given number has the same number of active and passive bits
void checkEqualBits(int num)
{
	if (num < 0)
	{
		return;
	}
	// Display given number
	printf("\n Number : %d", num);
	int bit = 0;
	while (num > 0)
	{
		if (num & 1 == 1)
		{
			// is active bit
			bit++;
		}
		else
		{
			// is an inactive bit
			bit--;
		}
		num = num >> 1;
	}
	if (bit == 0)
	{
		printf("\n Equal set and unset bits \n");
	}
	else
	{
		printf("\n Not equal set and unset bits\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	// 17 (10001)
	checkEqualBits(17);
	// 9 (1001)
	checkEqualBits(9);
	// 1 (1)
	checkEqualBits(1);
	// 7 (111)
	checkEqualBits(7);
	// 10 (1010)
	checkEqualBits(10);
	return 0;
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
/*
  Java Program 
  Count the number of active and inactive bits of a number
*/
public class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	public void checkEqualBits(int num)
	{
		if (num < 0)
		{
			return;
		}
		// Display given number
		System.out.print("\n Number : " + num);
		int bit = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// is active bit
				bit++;
			}
			else
			{
				// is an inactive bit
				bit--;
			}
			num = num >> 1;
		}
		if (bit == 0)
		{
			System.out.print("\n Equal set and unset bits \n");
		}
		else
		{
			System.out.print("\n Not equal set and unset bits\n");
		}
	}
	public static void main(String[] args)
	{
		Comparison task = new Comparison();
		// Test Cases
		// 17 (10001)
		task.checkEqualBits(17);
		// 9 (1001)
		task.checkEqualBits(9);
		// 1 (1)
		task.checkEqualBits(1);
		// 7 (111)
		task.checkEqualBits(7);
		// 10 (1010)
		task.checkEqualBits(10);
	}
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	public:
		// Check whether the given number has the same number of active and passive bits
		void checkEqualBits(int num)
		{
			if (num < 0)
			{
				return;
			}
			// Display given number
			cout << "\n Number : " << num;
			int bit = 0;
			while (num > 0)
			{
				if ((num &1) == 1)
				{
					// is active bit
					bit++;
				}
				else
				{
					// is an inactive bit
					bit--;
				}
				num = num >> 1;
			}
			if (bit == 0)
			{
				cout << "\n Equal set and unset bits \n";
			}
			else
			{
				cout << "\n Not equal set and unset bits\n";
			}
		}
};
int main()
{
	Comparison task = Comparison();
	// Test Cases
	// 17 (10001)
	task.checkEqualBits(17);
	// 9 (1001)
	task.checkEqualBits(9);
	// 1 (1)
	task.checkEqualBits(1);
	// 7 (111)
	task.checkEqualBits(7);
	// 10 (1010)
	task.checkEqualBits(10);
	return 0;
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
// Include namespace system
using System;
/*
  C# Program 
  Count the number of active and inactive bits of a number
*/
public class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	public void checkEqualBits(int num)
	{
		if (num < 0)
		{
			return;
		}
		// Display given number
		Console.Write("\n Number : " + num);
		int bit = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// is active bit
				bit++;
			}
			else
			{
				// is an inactive bit
				bit--;
			}
			num = num >> 1;
		}
		if (bit == 0)
		{
			Console.Write("\n Equal set and unset bits \n");
		}
		else
		{
			Console.Write("\n Not equal set and unset bits\n");
		}
	}
	public static void Main(String[] args)
	{
		Comparison task = new Comparison();
		// Test Cases
		// 17 (10001)
		task.checkEqualBits(17);
		// 9 (1001)
		task.checkEqualBits(9);
		// 1 (1)
		task.checkEqualBits(1);
		// 7 (111)
		task.checkEqualBits(7);
		// 10 (1010)
		task.checkEqualBits(10);
	}
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
<?php
/*
  Php Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	public	function checkEqualBits($num)
	{
		if ($num < 0)
		{
			return;
		}
		// Display given number
		echo "\n Number : ". $num;
		$bit = 0;
		while ($num > 0)
		{
			if (($num & 1) == 1)
			{
				// is active bit
				$bit++;
			}
			else
			{
				// is an inactive bit
				$bit--;
			}
			$num = $num >> 1;
		}
		if ($bit == 0)
		{
			echo "\n Equal set and unset bits \n";
		}
		else
		{
			echo "\n Not equal set and unset bits\n";
		}
	}
}

function main()
{
	$task = new Comparison();
	// Test Cases
	// 17 (10001)
	$task->checkEqualBits(17);
	// 9 (1001)
	$task->checkEqualBits(9);
	// 1 (1)
	$task->checkEqualBits(1);
	// 7 (111)
	$task->checkEqualBits(7);
	// 10 (1010)
	$task->checkEqualBits(10);
}
main();

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
/*
  Node Js Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	checkEqualBits(num)
	{
		if (num < 0)
		{
			return;
		}
		// Display given number
		process.stdout.write("\n Number : " + num);
		var bit = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// is active bit
				bit++;
			}
			else
			{
				// is an inactive bit
				bit--;
			}
			num = num >> 1;
		}
		if (bit == 0)
		{
			process.stdout.write("\n Equal set and unset bits \n");
		}
		else
		{
			process.stdout.write("\n Not equal set and unset bits\n");
		}
	}
}

function main()
{
	var task = new Comparison();
	// Test Cases
	// 17 (10001)
	task.checkEqualBits(17);
	// 9 (1001)
	task.checkEqualBits(9);
	// 1 (1)
	task.checkEqualBits(1);
	// 7 (111)
	task.checkEqualBits(7);
	// 10 (1010)
	task.checkEqualBits(10);
}
main();

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
#   Python 3 Program 
#   Count the number of active and inactive bits of a number

class Comparison :
	#  Check whether the given number has the same number of active and passive bits
	def checkEqualBits(self, num) :
		if (num < 0) :
			return
		
		#  Display given number
		print("\n Number : ", num, end = "")
		bit = 0
		while (num > 0) :
			if ((num & 1) == 1) :
				#  is active bit
				bit += 1
			else :
				#  is an inactive bit
				bit -= 1
			
			num = num >> 1
		
		if (bit == 0) :
			print("\n Equal set and unset bits ")
		else :
			print("\n Not equal set and unset bits")
		
	

def main() :
	task = Comparison()
	#  Test Cases
	#  17 (10001)
	task.checkEqualBits(17)
	#  9 (1001)
	task.checkEqualBits(9)
	#  1 (1)
	task.checkEqualBits(1)
	#  7 (111)
	task.checkEqualBits(7)
	#  10 (1010)
	task.checkEqualBits(10)

if __name__ == "__main__": main()

Output

 Number :  17
 Not equal set and unset bits

 Number :  9
 Equal set and unset bits

 Number :  1
 Not equal set and unset bits

 Number :  7
 Not equal set and unset bits

 Number :  10
 Equal set and unset bits
#   Ruby Program 
#   Count the number of active and inactive bits of a number

class Comparison 
	#  Check whether the given number has the same number of active and passive bits
	def checkEqualBits(num) 
		if (num < 0) 
			return
		end

		#  Display given number
		print("\n Number : ", num)
		bit = 0
		while (num > 0) 
			if ((num & 1) == 1) 
				#  is active bit
				bit += 1
			else 
				#  is an inactive bit
				bit -= 1
			end

			num = num >> 1
		end

		if (bit == 0) 
			print("\n Equal set and unset bits \n")
		else 
			print("\n Not equal set and unset bits\n")
		end

	end

end

def main() 
	task = Comparison.new()
	#  Test Cases
	#  17 (10001)
	task.checkEqualBits(17)
	#  9 (1001)
	task.checkEqualBits(9)
	#  1 (1)
	task.checkEqualBits(1)
	#  7 (111)
	task.checkEqualBits(7)
	#  10 (1010)
	task.checkEqualBits(10)
end

main()

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits 

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits 
/*
  Scala Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	def checkEqualBits(n: Int): Unit = {
		var num = n;
     	if (num < 0)
		{
			return;
		}
      	
		// Display given number
		print("\n Number : " + num);
		
		var bit: Int = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// is active bit
				bit += 1;
			}
			else
			{
				// is an inactive bit
				bit -= 1;
			}
			num = num >> 1;
		}
		if (bit == 0)
		{
			print("\n Equal set and unset bits \n");
		}
		else
		{
			print("\n Not equal set and unset bits\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Comparison = new Comparison();
		// Test Cases
		// 17 (10001)
		task.checkEqualBits(17);
		// 9 (1001)
		task.checkEqualBits(9);
		// 1 (1)
		task.checkEqualBits(1);
		// 7 (111)
		task.checkEqualBits(7);
		// 10 (1010)
		task.checkEqualBits(10);
	}
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
/*
  Swift 4 Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	func checkEqualBits(_ n: Int)
	{
      	var num = n;
		if (num < 0)
		{
			return;
		}
		// Display given number
		print("\n Number : ", num, terminator: "");
		var bit: Int = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// is active bit
				bit += 1;
			}
			else
			{
				// is an inactive bit
				bit -= 1;
			}
			num = num >> 1;
		}
		if (bit == 0)
		{
			print("\n Equal set and unset bits ");
		}
		else
		{
			print("\n Not equal set and unset bits");
		}
	}
}
func main()
{
	let task: Comparison = Comparison();
	// Test Cases
	// 17 (10001)
	task.checkEqualBits(17);
	// 9 (1001)
	task.checkEqualBits(9);
	// 1 (1)
	task.checkEqualBits(1);
	// 7 (111)
	task.checkEqualBits(7);
	// 10 (1010)
	task.checkEqualBits(10);
}
main();

Output

 Number :  17
 Not equal set and unset bits

 Number :  9
 Equal set and unset bits

 Number :  1
 Not equal set and unset bits

 Number :  7
 Not equal set and unset bits

 Number :  10
 Equal set and unset bits
/*
  Kotlin Program 
  Count the number of active and inactive bits of a number
*/
class Comparison
{
	// Check whether the given number has the same number of active and passive bits
	fun checkEqualBits(n: Int): Unit
	{
        var num : Int = n;
		if (num < 0)
		{
			return;
		}
		// Display given number
		print("\n Number : " + num);
		var bit: Int = 0;
		while (num > 0)
		{
			if ((num and 1) == 1)
			{
				// is active bit
				bit += 1;
			}
			else
			{
				// is an inactive bit
				bit -= 1;
			}
			num = num shr 1;
		}
		if (bit == 0)
		{
			print("\n Equal set and unset bits \n");
		}
		else
		{
			print("\n Not equal set and unset bits\n");
		}
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Comparison = Comparison();
	// Test Cases
	// 17 (10001)
	task.checkEqualBits(17);
	// 9 (1001)
	task.checkEqualBits(9);
	// 1 (1)
	task.checkEqualBits(1);
	// 7 (111)
	task.checkEqualBits(7);
	// 10 (1010)
	task.checkEqualBits(10);
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits
fn main()
{
	// Test Cases
	// 17 (10001)
	check_equal_bits(17);
	// 9 (1001)
	check_equal_bits(9);
	// 1 (1)
	check_equal_bits(1);
	// 7 (111)
	check_equal_bits(7);
	// 10 (1010)
	check_equal_bits(10);
}
fn check_equal_bits(n: i32)
{
  	let mut num: i32 = n;
	if num < 0
	{
		return;
	}
	// Display given number
	print!("\n Number : {}", num);
	let mut bit: i32 = 0;
	while num > 0
	{
		if num & 1 == 1
		{
			bit += 1;
		}
		else
		{
			bit -= 1;
		}
		num = num >> 1;
	}
	if bit == 0
	{
		print!("\n Equal set and unset bits \n");
	}
	else
	{
		print!("\n Not equal set and unset bits\n");
	}
}

Output

 Number : 17
 Not equal set and unset bits

 Number : 9
 Equal set and unset bits

 Number : 1
 Not equal set and unset bits

 Number : 7
 Not equal set and unset bits

 Number : 10
 Equal set and unset bits




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