Count total unset bits in all numbers from 1 to n

Here given code implementation process.

// C Program 
// Count total unset bits in all numbers from 1 to n
#include <stdio.h>

// Handles the request of count the all unset bits from (1 to n)
void countUnsetBit(int n)
{
	int result = 0;
	int x = 0;
	// Execute loop from 1 to n
	for (int i = 1; i <= n; ++i)
	{
		x = i;
		// Count unset bit of x number
		while (x > 0)
		{
			if (x % 2 == 0)
			{
				result++;
			}
			x /= 2;
		}
	}
	// Display result
	printf(" Unset bits from (1 to %d) : %d \n", n, result);
}
int main()
{
	// Test Case
	countUnsetBit(7);
	countUnsetBit(15);
	countUnsetBit(10);
	return 0;
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
/*
   Java Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	public void countUnsetBit(int n)
	{
		int result = 0;
		int x = 0;
		// Execute loop from 1 to n
		for (int i = 1; i <= n; ++i)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result++;
				}
				x /= 2;
			}
		}
		// Display result
		System.out.print(" Unset bits from (1 to " + n + ") : " + result + " \n");
	}
	public static void main(String[] args)
	{
		BitCounter task = new BitCounter();
		// Test Case
		task.countUnsetBit(7);
		task.countUnsetBit(15);
		task.countUnsetBit(10);
	}
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program for
   Count total unset bits in all numbers from 1 to n
*/

class BitCounter
{
	public:
		// Handles the request of count the all unset bits from (1 to n)
		void countUnsetBit(int n)
		{
			int result = 0;
			int x = 0;
			// Execute loop from 1 to n
			for (int i = 1; i <= n; ++i)
			{
				x = i;
				// Count unset bit of x number
				while (x > 0)
				{
					if (x % 2 == 0)
					{
						result++;
					}
					x /= 2;
				}
			}
			// Display result
			cout << " Unset bits from (1 to " << n << ") : " << result << " \n";
		}
};
int main()
{
	BitCounter task = BitCounter();
	// Test Case
	task.countUnsetBit(7);
	task.countUnsetBit(15);
	task.countUnsetBit(10);
	return 0;
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
// Include namespace system
using System;
/*
   C# Program for
   Count total unset bits in all numbers from 1 to n
*/
public class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	public void countUnsetBit(int n)
	{
		int result = 0;
		int x = 0;
		// Execute loop from 1 to n
		for (int i = 1; i <= n; ++i)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result++;
				}
				x /= 2;
			}
		}
		// Display result
		Console.Write(" Unset bits from (1 to " + n + ") : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		BitCounter task = new BitCounter();
		// Test Case
		task.countUnsetBit(7);
		task.countUnsetBit(15);
		task.countUnsetBit(10);
	}
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
<?php
/*
   Php Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	public	function countUnsetBit($n)
	{
		$result = 0;
		$x = 0;
		// Execute loop from 1 to n
		for ($i = 1; $i <= $n; ++$i)
		{
			$x = $i;
			// Count unset bit of x number
			while ($x > 0)
			{
				if ($x % 2 == 0)
				{
					$result++;
				}
				$x = intval($x / 2);
			}
		}
		// Display result
		echo " Unset bits from (1 to ". $n .") : ". $result ." \n";
	}
}

function main()
{
	$task = new BitCounter();
	// Test Case
	$task->countUnsetBit(7);
	$task->countUnsetBit(15);
	$task->countUnsetBit(10);
}
main();

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
/*
   Node Js Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	countUnsetBit(n)
	{
		var result = 0;
		var x = 0;
		// Execute loop from 1 to n
		for (var i = 1; i <= n; ++i)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result++;
				}
				x = parseInt(x / 2);
			}
		}
		// Display result
		process.stdout.write(" Unset bits from (1 to " + n + ") : " + result + " \n");
	}
}

function main()
{
	var task = new BitCounter();
	// Test Case
	task.countUnsetBit(7);
	task.countUnsetBit(15);
	task.countUnsetBit(10);
}
main();

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
#    Python 3 Program for
#    Count total unset bits in all numbers from 1 to n

class BitCounter :
	#  Handles the request of count the all unset bits from (1 to n)
	def countUnsetBit(self, n) :
		result = 0
		x = 0
		i = 1
		#  Execute loop from 1 to n
		while (i <= n) :
			x = i
			#  Count unset bit of x number
			while (x > 0) :
				if (x % 2 == 0) :
					result += 1
				
				x = int(x / 2)
			
			i += 1
		
		#  Display result
		print(" Unset bits from (1 to ", n ,") : ", result ," ")
	

def main() :
	task = BitCounter()
	#  Test Case
	task.countUnsetBit(7)
	task.countUnsetBit(15)
	task.countUnsetBit(10)

if __name__ == "__main__": main()

Output

 Unset bits from (1 to  7 ) :  5
 Unset bits from (1 to  15 ) :  17
 Unset bits from (1 to  10 ) :  12
#    Ruby Program for
#    Count total unset bits in all numbers from 1 to n

class BitCounter 
	#  Handles the request of count the all unset bits from (1 to n)
	def countUnsetBit(n) 
		result = 0
		x = 0
		i = 1
		#  Execute loop from 1 to n
		while (i <= n) 
			x = i
			#  Count unset bit of x number
			while (x > 0) 
				if (x % 2 == 0) 
					result += 1
				end

				x /= 2
			end

			i += 1
		end

		#  Display result
		print(" Unset bits from (1 to ", n ,") : ", result ," \n")
	end

end

def main() 
	task = BitCounter.new()
	#  Test Case
	task.countUnsetBit(7)
	task.countUnsetBit(15)
	task.countUnsetBit(10)
end

main()

Output

 Unset bits from (1 to 7) : 5 
 Unset bits from (1 to 15) : 17 
 Unset bits from (1 to 10) : 12 
/*
   Scala Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	def countUnsetBit(n: Int): Unit = {
		var result: Int = 0;
		var x: Int = 0;
		var i: Int = 1;
		// Execute loop from 1 to n
		while (i <= n)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result += 1;
				}
				x = (x / 2).toInt;
			}
			i += 1;
		}
		// Display result
		print(" Unset bits from (1 to " + n + ") : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitCounter = new BitCounter();
		// Test Case
		task.countUnsetBit(7);
		task.countUnsetBit(15);
		task.countUnsetBit(10);
	}
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12
/*
   Swift 4 Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	func countUnsetBit(_ n: Int)
	{
		var result: Int = 0;
		var x: Int = 0;
		var i: Int = 1;
		// Execute loop from 1 to n
		while (i <= n)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result += 1;
				}
				x /= 2;
			}
			i += 1;
		}
		// Display result
		print(" Unset bits from (1 to ", n ,") : ", result ," ");
	}
}
func main()
{
	let task: BitCounter = BitCounter();
	// Test Case
	task.countUnsetBit(7);
	task.countUnsetBit(15);
	task.countUnsetBit(10);
}
main();

Output

 Unset bits from (1 to  7 ) :  5
 Unset bits from (1 to  15 ) :  17
 Unset bits from (1 to  10 ) :  12
/*
   Kotlin Program for
   Count total unset bits in all numbers from 1 to n
*/
class BitCounter
{
	// Handles the request of count the all unset bits from (1 to n)
	fun countUnsetBit(n: Int): Unit
	{
		var result: Int = 0;
		var x: Int ;
		var i: Int = 1;
		// Execute loop from 1 to n
		while (i <= n)
		{
			x = i;
			// Count unset bit of x number
			while (x > 0)
			{
				if (x % 2 == 0)
				{
					result += 1;
				}
				x /= 2;
			}
			i += 1;
		}
		// Display result
		print(" Unset bits from (1 to " + n + ") : " + result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitCounter = BitCounter();
	// Test Case
	task.countUnsetBit(7);
	task.countUnsetBit(15);
	task.countUnsetBit(10);
}

Output

 Unset bits from (1 to 7) : 5
 Unset bits from (1 to 15) : 17
 Unset bits from (1 to 10) : 12

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