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 1001

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find total bit in given number
#include <stdio.h>      
#include <math.h>
  
unsigned countBits(unsigned int number)
{      
      // log function in base 2 
      // take only integer part
      return (int)log2(number)+1;
}
  
// Driven program       
int main()
{
    unsigned int num = 65;
    printf("%d\n", countBits(num));
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to 
// find total bit
// in given number
import java.io.*;
  
class GFG 
{
    static int countBits(int number)
    
          
        // log function in base 2 
        // take only integer part
        return (int)(Math.log(number) / 
                     Math.log(2) + 1);
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int num = 65;
          
        System.out.println(countBits(num));
                                  
    }
}
  
// This code is contributed by vij

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# total bit in given number
import math
def countBits(number):
      
    # log function in base 2 
    # take only integer part
    return int((math.log(number) / 
                math.log(2)) + 1);
  
# Driver Code
num = 65;
print(countBits(num));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find total bit
// in given number
using System;
  
class GFG {
      
    static uint countBits(uint number)
    {     
          
        // log function in base 2 
        // take only integer part
        return (uint)Math.Log(number , 2.0) + 1;
    }
      
    // Driver code
    public static void Main() 
    {
        uint num = 65;
          
        Console.WriteLine(countBits(num));
                                  
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find total
// bit in given number
  
function countBits($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.
?>

chevron_right



Output:

7

 

Method 2 (Using Bit Traversal)


C

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Function to get no of bits in binary
   representation of positive integer */
#include <stdio.h>         
     
unsigned int countBits(unsigned int n)
{
   unsigned int count = 0;
   while (n)
   {
        count++;
        n >>= 1;
    }
    return count;
}
   
/* Driver program*/
int main()
{
    int i = 65;
    printf("%d", countBits(i));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Function to get no of bits in binary
representation of positive integer */
class GFG {
  
    static int countBits(int n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n >>= 1;
        }
          
        return count;
    }
      
    /* Driver program*/
    public static void main(String[] arg)
    {
        int i = 65;
        System.out.print(countBits(i));
    }
}
  
// This code is contributed by Smitha.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to get no of bits 
# in binary representation 
# of positive integer 
  
def countBits(n):
  
    count = 0
    while (n):
        count += 1
        n >>= 1
          
    return count
  
# Driver program
i = 65
print(countBits(i))
  
# This code is contributed
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Function to get no of bits 
in binary representation of 
positive integer */
using System;
  
class GFG
{
    static int countBits(int n)
    {
        int count = 0;
        while (n != 0)
        {
            count++;
            n >>= 1;
        }
          
        return count;
    }
      
    // Driver Code
    static public void Main ()
    {
        int i = 65;
        Console.Write(countBits(i));
    }
}
  
// This code is contributed 
// by akt_mit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 
function countBits($n)
{
    $count = 0;
    while ($n)
    {
        $count++;
        $n >>= 1;
    }
    return $count;
}
  
// Driver Code
$i = 65;
echo(countBits($i));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

7

This article is contributed by Gyayak Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.