Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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++
// C++ program to find total bit in given number
#include <iostream>
#include <cmath>
unsigned countBits(unsigned intnumber)
{
// log function in base 2
// take only integer part
return(int)log2(number)+1;
}
// Driven program
intmain()
{
unsigned intnum = 65;
std::cout<<countBits(num)<<'\n';
return0;
}
// This code is contributed by thedev05.
C
// C program to find total bit in given number
#include <stdio.h>
#include <math.h>
unsigned countBits(unsigned intnumber)
{
// log function in base 2
// take only integer part
return(int)log2(number)+1;
}
// Driven program
intmain()
{
unsigned intnum = 65;
printf("%d\n", countBits(num));
return0;
}
Java
// Java program to
// find total bit
// in given number
importjava.io.*;
classGFG
{
staticintcountBits(intnumber)
{
// log function in base 2
// take only integer part
return(int)(Math.log(number) /
Math.log(2) + 1);
}
// Driver code
publicstaticvoidmain (String[] args)
{
intnum = 65;
System.out.println(countBits(num));
}
}
// This code is contributed by vij
Python3
# Python3 program to find
# total bit in given number
importmath
defcountBits(number):
# log function in base 2
# take only integer part
returnint((math.log(number) /
math.log(2)) +1);
# Driver Code
num =65;
print(countBits(num));
# This code is contributed by mits
C#
// C# program to find total bit
// in given number
usingSystem;
classGFG {
staticuintcountBits(uintnumber)
{
// log function in base 2
// take only integer part
return(uint)Math.Log(number , 2.0) + 1;
}
// Driver code
publicstaticvoidMain()
{
uintnum = 65;
Console.WriteLine(countBits(num));
}
}
// This code is contributed by Sam007.
PHP
<?php
// PHP program to find total
// bit in given number
functioncountBits($number)
{
// log function in base 2
// take only integer part
return(int)(log($number) /
log(2)) + 1;
}
// Driver Code
$num= 65;
echo(countBits($num));
// This code is contributed by Ajit.
?>
Javascript
<script>
// JavaScript program to find total bit in given number
functioncountBits(number) {
// log function in base 2
// take only integer part
returnMath.floor(Math.log2(number)+1);
}
// Driven program
let num = 65;
document.write(countBits(num));
// This code is contributed by Surbhi Tyagi
</script>
Output
7
Method 2 (Using Bit Traversal)
C
/* Function to get no of bits in binary
representation of positive integer */
#include <stdio.h>
unsigned intcountBits(unsigned intn)
{
unsigned intcount = 0;
while(n)
{
count++;
n >>= 1;
}
returncount;
}
/* Driver program*/
intmain()
{
inti = 65;
printf("%d", countBits(i));
return0;
}
Java
/* Function to get no of bits in binary
representation of positive integer */
classGFG {
staticintcountBits(intn)
{
intcount = 0;
while(n != 0)
{
count++;
n >>= 1;
}
returncount;
}
/* Driver program*/
publicstaticvoidmain(String[] arg)
{
inti = 65;
System.out.print(countBits(i));
}
}
// This code is contributed by Smitha.
Python3
# Function to get no of bits
# in binary representation
# of positive integer
defcountBits(n):
count =0
while(n):
count +=1
n >>=1
returncount
# Driver program
i =65
print(countBits(i))
# This code is contributed
# by Smitha
C#
/* Function to get no of bits
in binary representation of
positive integer */
usingSystem;
classGFG
{
staticintcountBits(intn)
{
intcount = 0;
while(n != 0)
{
count++;
n >>= 1;
}
returncount;
}
// Driver Code
staticpublicvoidMain ()
{
inti = 65;
Console.Write(countBits(i));
}
}
// This code is contributed
// by akt_mit.
PHP
<?php
// PHP Code to get no of bits in binary
// representation of positive integer
// Function to get no of bits in binary
// representation of positive integer
functioncountBits($n)
{
$count= 0;
while($n)
{
$count++;
$n>>= 1;
}
return$count;
}
// Driver Code
$i= 65;
echo(countBits($i));
// This code is contributed by Ajit.
?>
Javascript
<script>
/* Function to get no of bits
in binary representation of
positive integer */
functioncountBits(n)
{
varcount = 0;
while(n != 0)
{
count++;
n >>= 1;
}
returncount;
}
// Driver Code
vari = 65;
document.write(countBits(i));
</script>
Output
7
Method 3 ( Using conversion from binary to string)
Python3
a =65
b =183
# Convert an integer to binary
a_bit =bin(a)[2::]
b_bit =bin(b)[2::]
# Convert the binary to string so that we can count the bits
str_a_bit =str(a_bit)
str_b_bit =str(b_bit)
print("Total bits :", len(str_a_bit))
print("Total bits :", len(str_b_bit))
# This code is contributed by harshit
Output
Total bits : 7
Total bits : 8
This article is contributed by Gyayak Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy