Count the number of active and inactive bits of a number

Here given code implementation process.

// C Program 
// Count the number of active and inactive bits of a number
#include <stdio.h>


// Count the number of set and unset bits of a number
void countSetUnsetBit(int num)
{
    if(num < 0)
    {
        return;
    }
    // Display given number
    printf("\n Number : %d",num);

    int activeBit = 0;
    int inActiveBit = 0;

    while(num > 0)
    {
        if(num & 1==1)
        {
            activeBit++;
        }
        else
        {
            inActiveBit++;
        }
        num = num >> 1;
    }
    // Display calculated result
    printf("\n Active Bits : %d",activeBit);
    printf("\n Inactive Bits : %d\n",inActiveBit);
}
int main(int argc, char const *argv[])
{

    // Test Cases
    // 17 (10001)
    countSetUnsetBit(17);
    // 34 (100010)
    countSetUnsetBit(34);
    // 1 (1)
    countSetUnsetBit(1);
    // 7 (111)
    countSetUnsetBit(7);
    // 21 (10101)
    countSetUnsetBit(21);
    return 0;
}

Output

 Number : 17
 Active Bits : 2
 Inactive Bits : 3

 Number : 34
 Active Bits : 2
 Inactive Bits : 4

 Number : 1
 Active Bits : 1
 Inactive Bits : 0

 Number : 7
 Active Bits : 3
 Inactive Bits : 0

 Number : 21
 Active Bits : 3
 Inactive Bits : 2
/*
  Java Program 
  Count the number of active and inactive bits of a number
*/
public class BitCounting
{
    // Count the number of set and unset bits of a number
    public void countSetUnsetBit(int num)
    {
        if (num < 0)
        {
            return;
        }
        // Display given number
        System.out.print("\n Number : " + num );
        int activeBit = 0;
        int inActiveBit = 0;
        while (num > 0)
        {
            if ((num & 1) == 1)
            {
                activeBit++;
            }
            else
            {
                inActiveBit++;
            }
            num = num >> 1;
        }
        // Display calculated result
        System.out.print("\n Active Bits   : " + activeBit );
        System.out.print("\n Inactive Bits : " + inActiveBit + "\n");
    }
    public static void main(String[] args)
    {
        BitCounting task = new BitCounting();
        // Test Cases
        // 17 (10001)
        task.countSetUnsetBit(17);
        // 34 (100010)
        task.countSetUnsetBit(34);
        // 1 (1)
        task.countSetUnsetBit(1);
        // 7 (111)
        task.countSetUnsetBit(7);
        // 21 (10101)
        task.countSetUnsetBit(21);
    }
}

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	public:
		// Count the number of set and unset bits of a number
		void countSetUnsetBit(int num)
		{
			if (num < 0)
			{
				return;
			}
			// Display given number
			cout << "\n Number : " << num;
			int activeBit = 0;
			int inActiveBit = 0;
			while (num > 0)
			{
				if ((num &1) == 1)
				{
					activeBit++;
				}
				else
				{
					inActiveBit++;
				}
				num = num >> 1;
			}
			// Display calculated result
			cout << "\n Active Bits   : " << activeBit;
			cout << "\n Inactive Bits : " << inActiveBit << "\n";
		}
};
int main()
{
	BitCounting task = BitCounting();
	// Test Cases
	// 17 (10001)
	task.countSetUnsetBit(17);
	// 34 (100010)
	task.countSetUnsetBit(34);
	// 1 (1)
	task.countSetUnsetBit(1);
	// 7 (111)
	task.countSetUnsetBit(7);
	// 21 (10101)
	task.countSetUnsetBit(21);
	return 0;
}

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
// Include namespace system
using System;
/*
  C# Program 
  Count the number of active and inactive bits of a number
*/
public class BitCounting
{
	// Count the number of set and unset bits of a number
	public void countSetUnsetBit(int num)
	{
		if (num < 0)
		{
			return;
		}
		// Display given number
		Console.Write("\n Number : " + num);
		int activeBit = 0;
		int inActiveBit = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				activeBit++;
			}
			else
			{
				inActiveBit++;
			}
			num = num >> 1;
		}
		// Display calculated result
		Console.Write("\n Active Bits   : " + activeBit);
		Console.Write("\n Inactive Bits : " + inActiveBit + "\n");
	}
	public static void Main(String[] args)
	{
		BitCounting task = new BitCounting();
		// Test Cases
		// 17 (10001)
		task.countSetUnsetBit(17);
		// 34 (100010)
		task.countSetUnsetBit(34);
		// 1 (1)
		task.countSetUnsetBit(1);
		// 7 (111)
		task.countSetUnsetBit(7);
		// 21 (10101)
		task.countSetUnsetBit(21);
	}
}

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
<?php
/*
  Php Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	// Count the number of set and unset bits of a number
	public	function countSetUnsetBit($num)
	{
		if ($num < 0)
		{
			return;
		}
		// Display given number
		echo "\n Number : ". $num;
		$activeBit = 0;
		$inActiveBit = 0;
		while ($num > 0)
		{
			if (($num & 1) == 1)
			{
				$activeBit++;
			}
			else
			{
				$inActiveBit++;
			}
			$num = $num >> 1;
		}
		// Display calculated result
		echo "\n Active Bits   : ". $activeBit;
		echo "\n Inactive Bits : ". $inActiveBit ."\n";
	}
}

function main()
{
	$task = new BitCounting();
	// Test Cases
	// 17 (10001)
	$task->countSetUnsetBit(17);
	// 34 (100010)
	$task->countSetUnsetBit(34);
	// 1 (1)
	$task->countSetUnsetBit(1);
	// 7 (111)
	$task->countSetUnsetBit(7);
	// 21 (10101)
	$task->countSetUnsetBit(21);
}
main();

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
/*
  Node Js Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	// Count the number of set and unset bits of a number
	countSetUnsetBit(num)
	{
		if (num < 0)
		{
			return;
		}
		// Display given number
		process.stdout.write("\n Number : " + num);
		var activeBit = 0;
		var inActiveBit = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				activeBit++;
			}
			else
			{
				inActiveBit++;
			}
			num = num >> 1;
		}
		// Display calculated result
		process.stdout.write("\n Active Bits   : " + activeBit);
		process.stdout.write("\n Inactive Bits : " + inActiveBit + "\n");
	}
}

function main()
{
	var task = new BitCounting();
	// Test Cases
	// 17 (10001)
	task.countSetUnsetBit(17);
	// 34 (100010)
	task.countSetUnsetBit(34);
	// 1 (1)
	task.countSetUnsetBit(1);
	// 7 (111)
	task.countSetUnsetBit(7);
	// 21 (10101)
	task.countSetUnsetBit(21);
}
main();

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
#   Python 3 Program 
#   Count the number of active and inactive bits of a number

class BitCounting :
	#  Count the number of set and unset bits of a number
	def countSetUnsetBit(self, num) :
		if (num < 0) :
			return
		
		#  Display given number
		print("\n Number : ", num, end = "")
		activeBit = 0
		inActiveBit = 0
		while (num > 0) :
			if ((num & 1) == 1) :
				activeBit += 1
			else :
				inActiveBit += 1
			
			num = num >> 1
		
		#  Display calculated result
		print("\n Active Bits   : ", activeBit, end = "")
		print("\n Inactive Bits : ", inActiveBit )
	

def main() :
	task = BitCounting()
	#  Test Cases
	#  17 (10001)
	task.countSetUnsetBit(17)
	#  34 (100010)
	task.countSetUnsetBit(34)
	#  1 (1)
	task.countSetUnsetBit(1)
	#  7 (111)
	task.countSetUnsetBit(7)
	#  21 (10101)
	task.countSetUnsetBit(21)

if __name__ == "__main__": main()

Output

 Number :  17
 Active Bits   :  2
 Inactive Bits :  3

 Number :  34
 Active Bits   :  2
 Inactive Bits :  4

 Number :  1
 Active Bits   :  1
 Inactive Bits :  0

 Number :  7
 Active Bits   :  3
 Inactive Bits :  0

 Number :  21
 Active Bits   :  3
 Inactive Bits :  2
#   Ruby Program 
#   Count the number of active and inactive bits of a number

class BitCounting 
	#  Count the number of set and unset bits of a number
	def countSetUnsetBit(num) 
		if (num < 0) 
			return
		end

		#  Display given number
		print("\n Number : ", num)
		activeBit = 0
		inActiveBit = 0
		while (num > 0) 
			if ((num & 1) == 1) 
				activeBit += 1
			else 
				inActiveBit += 1
			end

			num = num >> 1
		end

		#  Display calculated result
		print("\n Active Bits   : ", activeBit)
		print("\n Inactive Bits : ", inActiveBit ,"\n")
	end

end

def main() 
	task = BitCounting.new()
	#  Test Cases
	#  17 (10001)
	task.countSetUnsetBit(17)
	#  34 (100010)
	task.countSetUnsetBit(34)
	#  1 (1)
	task.countSetUnsetBit(1)
	#  7 (111)
	task.countSetUnsetBit(7)
	#  21 (10101)
	task.countSetUnsetBit(21)
end

main()

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
/*
  Scala Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	// Count the number of set and unset bits of a number
	def countSetUnsetBit(n: Int): Unit = {
		var num = n;
      	if (num < 0)
		{
			return;
		}
		// Display given number
		print("\n Number : " + num);
		var activeBit: Int = 0;
		var inActiveBit: Int = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				activeBit += 1;
			}
			else
			{
				inActiveBit += 1;
			}
			num = num >> 1;
		}
		// Display calculated result
		print("\n Active Bits   : " + activeBit);
		print("\n Inactive Bits : " + inActiveBit + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitCounting = new BitCounting();
		// Test Cases
		// 17 (10001)
		task.countSetUnsetBit(17);
		// 34 (100010)
		task.countSetUnsetBit(34);
		// 1 (1)
		task.countSetUnsetBit(1);
		// 7 (111)
		task.countSetUnsetBit(7);
		// 21 (10101)
		task.countSetUnsetBit(21);
	}
}

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2
/*
  Swift 4 Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	// Count the number of set and unset bits of a number
	func countSetUnsetBit(_ n: Int)
	{
      	var num = n;
		if (num < 0)
		{
			return;
		}
		// Display given number
		print("\n Number : ", num, terminator: "");
		var activeBit: Int = 0;
		var inActiveBit: Int = 0;
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				activeBit += 1;
			}
			else
			{
				inActiveBit += 1;
			}
			num = num >> 1;
		}
		// Display calculated result
		print("\n Active Bits   : ", activeBit, terminator: "");
		print("\n Inactive Bits : ", inActiveBit );
	}
}
func main()
{
	let task: BitCounting = BitCounting();
	// Test Cases
	// 17 (10001)
	task.countSetUnsetBit(17);
	// 34 (100010)
	task.countSetUnsetBit(34);
	// 1 (1)
	task.countSetUnsetBit(1);
	// 7 (111)
	task.countSetUnsetBit(7);
	// 21 (10101)
	task.countSetUnsetBit(21);
}
main();

Output

 Number :  17
 Active Bits   :  2
 Inactive Bits :  3

 Number :  34
 Active Bits   :  2
 Inactive Bits :  4

 Number :  1
 Active Bits   :  1
 Inactive Bits :  0

 Number :  7
 Active Bits   :  3
 Inactive Bits :  0

 Number :  21
 Active Bits   :  3
 Inactive Bits :  2
/*
  Kotlin Program 
  Count the number of active and inactive bits of a number
*/
class BitCounting
{
	// Count the number of set and unset bits of a number
	fun countSetUnsetBit(n: Int): Unit
	{
      	var num = n;
		if (num < 0)
		{
			return;
		}
		// Display given number
		print("\n Number : " + num);
		var activeBit: Int = 0;
		var inActiveBit: Int = 0;
		while (num > 0)
		{
			if ((num and 1) == 1)
			{
				activeBit += 1;
			}
			else
			{
				inActiveBit += 1;
			}
			num = num shr 1;
		}
		// Display calculated result
		print("\n Active Bits   : " + activeBit);
		print("\n Inactive Bits : " + inActiveBit + "\n");
	}
}
fun main(args: Array <String> ): Unit
{
	var task: BitCounting = BitCounting();
	// Test Cases
	// 17 (10001)
	task.countSetUnsetBit(17);
	// 34 (100010)
	task.countSetUnsetBit(34);
	// 1 (1)
	task.countSetUnsetBit(1);
	// 7 (111)
	task.countSetUnsetBit(7);
	// 21 (10101)
	task.countSetUnsetBit(21);
}

Output

 Number : 17
 Active Bits   : 2
 Inactive Bits : 3

 Number : 34
 Active Bits   : 2
 Inactive Bits : 4

 Number : 1
 Active Bits   : 1
 Inactive Bits : 0

 Number : 7
 Active Bits   : 3
 Inactive Bits : 0

 Number : 21
 Active Bits   : 3
 Inactive Bits : 2


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