Count total bits in a number
Given a positive number n, count total bit in it.
Examples:
Input : 13
Output : 4
Binary representation of 13 is 1101
Input : 183
Output : 8
Input : 4096
Output : 13
Method 1 (Using Log)
The log2(n) logarithm in base 2 of n, which is the exponent to which 2 is raised to get n only integer and we add 1 find total bit in a number in log(n) time.
C++
#include <iostream>
#include <cmath>
unsigned countBits(unsigned int number)
{
return ( int )log2(number)+1;
}
int main()
{
unsigned int num = 65;
std::cout<<countBits(num)<< '\n' ;
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
unsigned countBits(unsigned int number)
{
return ( int )log2(number)+1;
}
int main()
{
unsigned int num = 65;
printf ( "%d\n" , countBits(num));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countBits( int number)
{
return ( int )(Math.log(number) /
Math.log( 2 ) + 1 );
}
public static void main (String[] args)
{
int num = 65 ;
System.out.println(countBits(num));
}
}
|
Python3
import math
def countBits(number):
return int ((math.log(number) /
math.log( 2 )) + 1 );
num = 65 ;
print (countBits(num));
|
C#
using System;
class GFG {
static uint countBits( uint number)
{
return ( uint )Math.Log(number , 2.0) + 1;
}
public static void Main()
{
uint num = 65;
Console.WriteLine(countBits(num));
}
}
|
PHP
<?php
function countBits( $number )
{
return (int)(log( $number ) /
log(2)) + 1;
}
$num = 65;
echo (countBits( $num ));
?>
|
Javascript
<script>
function countBits(number) {
return Math.floor(Math.log2(number)+1);
}
let num = 65;
document.write(countBits(num));
</script>
|
Time Complexity : O(logn)
Auxiliary Space : O(1)
Method 2 (Using Bit Traversal)
C
#include <stdio.h>
unsigned int countBits(unsigned int n)
{
unsigned int count = 0;
while (n)
{
count++;
n >>= 1;
}
return count;
}
int main()
{
int i = 65;
printf ( "%d" , countBits(i));
return 0;
}
|
Java
class GFG {
static int countBits( int n)
{
int count = 0 ;
while (n != 0 )
{
count++;
n >>= 1 ;
}
return count;
}
public static void main(String[] arg)
{
int i = 65 ;
System.out.print(countBits(i));
}
}
|
Python3
C#
PHP
Javascript
Time Complexity : O(logn)
Auxiliary Space : O(1)
Method 3 ( Using conversion from binary to string)
C++
#include <bits/stdc++.h>
using namespace std;
int count_bits( int n)
{
string binary = bitset< 64 >(n).to_string();
return 64 - binary.find( '1' );
}
int main()
{
int a = 65;
int b = 183;
cout << "Total bits in " << a << " : " << count_bits(a) << endl;
cout << "Total bits in " << b << " : " << count_bits(b) << endl;
}
|
Java
class GFG {
static int count_bits( int n)
{
return Integer.toBinaryString(n).length();
}
public static void main(String[] args)
{
int a = 65 ;
int b = 183 ;
System.out.printf( "Total bits in %d: %d\n" , a,
count_bits(a));
System.out.printf( "Total bits in %d: %d\n" , b,
count_bits(b));
}
}
|
Python3
C#
Javascript
function count_bits(n)
{
let binary = n.toString(2);
return binary.length;
}
let a = 65;
let b = 183;
console.log( "Total bits in" , a, ":" , count_bits(a));
console.log( "Total bits in" , b, ":" , count_bits(b));
|
Output
Total bits : 7
Total bits : 8
Time Complexity : O(logn)
Auxiliary Space : O(1)
Last Updated :
15 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...