Count set bits in an integer

Here given code implementation process.

// C program for
// Count set bits in an integer
#include <stdio.h>
 // Returns the number of active set bits in positive integer
int setBit(int num)
{
	int count = 0;
	while (num > 0)
	{
		count++;
		num = num & (num - 1);
	}
	// Returning the value of calculate result
	return count;
}
// Handles the request to find set bits in given integer
void coutSetBit(int num)
{
	printf(" Number : %d\n", num);
	printf(" Set Bit : %d\n\n", setBit(num));
}
int main()
{
	// 1010
	coutSetBit(10);
	//1111
	coutSetBit(15);
	//101010101
	coutSetBit(341);
	return 0;
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
/*
  Java Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	public int setBit(int num)
	{
		int count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		// Returning the value of calculate result
		return count;
	}
	// Handles the request to find set bits in given integer
	public void coutSetBit(int num)
	{
		System.out.print(" Number : " + num + "\n");
		System.out.print(" Set Bit : " + setBit(num) + "\n\n");
	}
	public static void main(String[] args)
	{
		ActiveBits task = new ActiveBits();
		// 1010
		task.coutSetBit(10);
		//1111
		task.coutSetBit(15);
		//101010101
		task.coutSetBit(341);
	}
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Count set bits in an integer
*/

class ActiveBits
{
	public:
		// Returns the number of active set bits in positive integer
		int setBit(int num)
		{
			// Returning the value of calculate result
			int count = 0;
			while (num > 0)
			{
				count++;
				num = num & (num - 1);
			}
			return count;
		}
	// Handles the request to find set bits in given integer
	void coutSetBit(int num)
	{
		cout << " Number : " << num << "\n";
		cout << " Set Bit : " << this->setBit(num) << "\n\n";
	}
};
int main()
{
	ActiveBits task = ActiveBits();
	// 1010
	task.coutSetBit(10);
	//1111
	task.coutSetBit(15);
	//101010101
	task.coutSetBit(341);
	return 0;
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
// Include namespace system
using System;
/*
  C# Program for
  Count set bits in an integer
*/
public class ActiveBits
{
	// Returns the number of active set bits in positive integer
	public int setBit(int num)
	{
		// Returning the value of calculate result
		int count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		return count;
	}
	// Handles the request to find set bits in given integer
	public void coutSetBit(int num)
	{
		Console.Write(" Number : " + num + "\n");
		Console.Write(" Set Bit : " + setBit(num) + "\n\n");
	}
	public static void Main(String[] args)
	{
		ActiveBits task = new ActiveBits();
		// 1010
		task.coutSetBit(10);
		//1111
		task.coutSetBit(15);
		//101010101
		task.coutSetBit(341);
	}
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
<?php
/*
  Php Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	public	function setBit($num)
	{
		// Returning the value of calculate result
		$count = 0;
		while ($num > 0)
		{
			$count++;
			$num = $num & ($num - 1);
		}
		return $count;
	}
	// Handles the request to find set bits in given integer
	public	function coutSetBit($num)
	{
		echo " Number : ". $num ."\n";
		echo " Set Bit : ". $this->setBit($num) ."\n\n";
	}
}

function main()
{
	$task = new ActiveBits();
	// 1010
	$task->coutSetBit(10);
	//1111
	$task->coutSetBit(15);
	//101010101
	$task->coutSetBit(341);
}
main();

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
/*
  Node Js Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	setBit(num)
	{
		// Returning the value of calculate result
		var count = 0;
		while (num > 0)
		{
			count++;
			num = num & (num - 1);
		}
		return count;
	}
	// Handles the request to find set bits in given integer
	coutSetBit(num)
	{
		process.stdout.write(" Number : " + num + "\n");
		process.stdout.write(" Set Bit : " + this.setBit(num) + "\n\n");
	}
}

function main()
{
	var task = new ActiveBits();
	// 1010
	task.coutSetBit(10);
	//1111
	task.coutSetBit(15);
	//101010101
	task.coutSetBit(341);
}
main();

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
#   Python 3 Program for
#   Count set bits in an integer

class ActiveBits :
	#  Returns the number of active set bits in positive integer
	def setBit(self, num) :
		count = 0
		while (num > 0) :
			count += 1
			num = num & (num - 1)
		
		#  Returning the value of calculate result
		return count
	
	#  Handles the request to find set bits in given integer
	def coutSetBit(self, num) :
		print(" Number : ", num )
		print(" Set Bit : ", self.setBit(num) ,"\n")
	

def main() :
	task = ActiveBits()
	#  1010
	task.coutSetBit(10)
	# 1111
	task.coutSetBit(15)
	# 101010101
	task.coutSetBit(341)

if __name__ == "__main__": main()

Output

 Number :  10
 Set Bit :  2

 Number :  15
 Set Bit :  4

 Number :  341
 Set Bit :  5
#   Ruby Program for
#   Count set bits in an integer

class ActiveBits 
	#  Returns the number of active set bits in positive integer
	def setBit(num) 
		count = 0
		while (num > 0) 
			count += 1
			num = num & (num - 1)
		end

		#  Returning the value of calculate result
		return count
	end

	#  Handles the request to find set bits in given integer
	def coutSetBit(num) 
		print(" Number : ", num ,"\n")
		print(" Set Bit : ", self.setBit(num) ,"\n\n")
	end

end

def main() 
	task = ActiveBits.new()
	#  1010
	task.coutSetBit(10)
	# 1111
	task.coutSetBit(15)
	# 101010101
	task.coutSetBit(341)
end

main()

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5

/*
  Scala Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	def setBit(num: Int): Int = {
		// Returning the value of calculate result
		var count: Int = 0;
      	var n = num;
		while (n > 0)
		{
			count += 1;
			n = n & (n - 1);
		}
		return count;
	}
	// Handles the request to find set bits in given integer
	def coutSetBit(num: Int): Unit = {
		print(" Number : " + num + "\n");
		print(" Set Bit : " + this.setBit(num) + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ActiveBits = new ActiveBits();
		// 1010
		task.coutSetBit(10);
		//1111
		task.coutSetBit(15);
		//101010101
		task.coutSetBit(341);
	}
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5
/*
  Swift 4 Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	func setBit(_ num: Int)->Int
	{
		// Returning the value of calculate result
		var count: Int = 0;
      	var n: Int = num;
		while (n > 0)
		{
			count += 1;
			n = n & (n - 1);
		}
		return count;
	}
	// Handles the request to find set bits in given integer
	func coutSetBit(_ num: Int)
	{
		print(" Number : ", num );
		print(" Set Bit : ", self.setBit(num) ,"\n");
	}
}
func main()
{
	let task: ActiveBits = ActiveBits();
	// 1010
	task.coutSetBit(10);
	//1111
	task.coutSetBit(15);
	//101010101
	task.coutSetBit(341);
}
main();

Output

 Number :  10
 Set Bit :  2

 Number :  15
 Set Bit :  4

 Number :  341
 Set Bit :  5
/*
  Kotlin Program for
  Count set bits in an integer
*/
class ActiveBits
{
	// Returns the number of active set bits in positive integer
	fun setBit(num: Int): Int
	{
		// Returning the value of calculate result
		var count: Int = 0;
      	var n = num;
		while (n > 0)
		{
			count += 1;
			n = n and (n - 1);
		}
		return count;
	}
	// Handles the request to find set bits in given integer
	fun coutSetBit(num: Int): Unit
	{
		print(" Number : " + num + "\n");
		print(" Set Bit : " + this.setBit(num) + "\n\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: ActiveBits = ActiveBits();
	// 1010
	task.coutSetBit(10);
	//1111
	task.coutSetBit(15);
	//101010101
	task.coutSetBit(341);
}

Output

 Number : 10
 Set Bit : 2

 Number : 15
 Set Bit : 4

 Number : 341
 Set Bit : 5

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