Count total bits in a number

Here given code implementation process.

// C Program 
// Count total bits in a number
#include <stdio.h>
#include <math.h>

void countBits(int n)
{
	// Calculate total bits
	int result = (int)(log(n) / log(2) + 1);
	// Display result
	printf("\n Number : %d\n", n);
	printf(" Bits : %d\n", result);
}
int main()
{
	//Simple test
	// 111 
	countBits(7);
	// 10000
	countBits(16);
	// 1010
	countBits(10);
	return 0;
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
/*
   Java Program for
   Count total bits in a number
*/
class BitCounter
{
    // Count number of bits in a given number
    public void countBits(int n)
    {
        // Calculate total bits
        int result = (int) (Math.log(n) / Math.log(2)+1);
        // Display result
        System.out.print("\n Number : " + n + "\n");
        System.out.print(" Bits : " + result + "\n");
    }
    public static void main(String[] args)
    {
  
        BitCounter task = new  BitCounter();
        //Simple test
        // 111 
        task.countBits(7);
        // 10000
        task.countBits(16);
        // 1010
        task.countBits(10);
    }
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
// Include header file
#include <iostream>

#include<math.h>

using namespace std;
/*
   C++ Program for
   Count total bits in a number
*/
class BitCounter
{
	public:
		// Count number of bits in a given number
		void countBits(int n)
		{
			// Calculate total bits
			int result = (int)(log(n) / log(2) + 1);
			// Display result
			cout << "\n Number : " << n << "\n";
			cout << " Bits : " << result << "\n";
		}
};
int main()
{
	BitCounter task = BitCounter();
	//Simple test
	// 111
	task.countBits(7);
	// 10000
	task.countBits(16);
	// 1010
	task.countBits(10);
	return 0;
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
// Include namespace system
using System;
/*
   C# Program for
   Count total bits in a number
*/
public class BitCounter
{
	// Count number of bits in a given number
	public void countBits(int n)
	{
		// Calculate total bits
		int result = (int)(Math.Log(n) / Math.Log(2)+1);
		// Display result
		Console.Write("\n Number : " + n + "\n");
		Console.Write(" Bits : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		BitCounter task = new BitCounter();
		//Simple test
		// 111
		task.countBits(7);
		// 10000
		task.countBits(16);
		// 1010
		task.countBits(10);
	}
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
<?php
/*
   Php Program for
   Count total bits in a number
*/
class BitCounter
{
	// Count number of bits in a given number
	public	function countBits($n)
	{
		// Calculate total bits
		$result = (intval(log($n) / log(2)) + 1);
		// Display result
		echo "\n Number : ". $n ."\n";
		echo " Bits : ". $result ."\n";
	}
}

function main()
{
	$task = new BitCounter();
	//Simple test
	// 111
	$task->countBits(7);
	// 10000
	$task->countBits(16);
	// 1010
	$task->countBits(10);
}
main();

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
/*
   Node Js Program for
   Count total bits in a number
*/
class BitCounter
{
	// Count number of bits in a given number
	countBits(n)
	{
		// Calculate total bits
		var result = (parseInt(Math.log(n) / Math.log(2)) + 1);
		// Display result
		process.stdout.write("\n Number : " + n + "\n");
		process.stdout.write(" Bits : " + result + "\n");
	}
}

function main()
{
	var task = new BitCounter();
	//Simple test
	// 111
	task.countBits(7);
	// 10000
	task.countBits(16);
	// 1010
	task.countBits(10);
}
main();

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
import math
#    Python 3 Program for
#    Count total bits in a number

class BitCounter :
	#  Count number of bits in a given number
	def countBits(self, n) :
		#  Calculate total bits
		result = int((int(math.log(n) / math.log(2)) + 1))
		#  Display result
		print("\n Number : ", n )
		print(" Bits : ", result )
	

def main() :
	task = BitCounter()
	# Simple test
	#  111 
	task.countBits(7)
	#  10000
	task.countBits(16)
	#  1010
	task.countBits(10)

if __name__ == "__main__": main()

Output

 Number :  7
 Bits :  3

 Number :  16
 Bits :  5

 Number :  10
 Bits :  4
#    Ruby Program for
#    Count total bits in a number

class BitCounter 
	#  Count number of bits in a given number
	def countBits(n) 
		#  Calculate total bits
		result = ((Math.log(n) / Math.log(2) + 1)).to_i
		#  Display result
		print("\n Number : ", n ,"\n")
		print(" Bits : ", result ,"\n")
	end

end

def main() 
	task = BitCounter.new()
	# Simple test
	#  111 
	task.countBits(7)
	#  10000
	task.countBits(16)
	#  1010
	task.countBits(10)
end

main()

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
/*
   Scala Program for
   Count total bits in a number
*/
class BitCounter
{
	// Count number of bits in a given number
	def countBits(n: Int): Unit = {
		// Calculate total bits
		var result: Int = ((Math.log(n) / Math.log(2)).toInt + 1);
		// Display result
		print("\n Number : " + n + "\n");
		print(" Bits : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitCounter = new BitCounter();
		//Simple test
		// 111
		task.countBits(7);
		// 10000
		task.countBits(16);
		// 1010
		task.countBits(10);
	}
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 4
import Foundation
/*
   Swift 4 Program for
   Count total bits in a number
*/
class BitCounter
{
	// Count number of bits in a given number
	func countBits(_ n: Int)
	{
		// Calculate total bits
		let result: Int = Int((log(Double(n)) / log(Double(2)) + 1));
	// Display result
	print("\n Number : ", n );
	print(" Bits : ", result );
}
}
func main()
{
	let task: BitCounter = BitCounter();
	//Simple test
	// 111
	task.countBits(7);
	// 10000
	task.countBits(16);
	// 1010
	task.countBits(10);
}
main();

Output

 Number :  7
 Bits :  3

 Number :  16
 Bits :  5

 Number :  10
 Bits :  4
/*
   Kotlin Program for
   Count total bits in a number
*/
class BitCounter
{
	// Count number of bits in a given number
	fun countBits(n: Int): Unit
	{
		// Calculate total bits
		var result: Int = (Math.log(n.toDouble()) / Math.log(2.0) + 1).toInt();
		// Display result
		print("\n Number : " + n + "\n");
		print(" Bits : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: BitCounter = BitCounter();
	//Simple test
	// 111
	task.countBits(7);
	// 10000
	task.countBits(16);
	// 1010
	task.countBits(10);
}

Output

 Number : 7
 Bits : 3

 Number : 16
 Bits : 5

 Number : 10
 Bits : 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