📜  计算一个数字中的总位数

📅  最后修改于: 2021-04-29 01:37:04             🧑  作者: Mango

给定一个正数n,计算其中的总位数。
例子:

Input : 13
Output : 4
Binary representation of 13 is 1101

Input  : 183
Output : 8

Input  : 4096
Output : 13

方法1(使用日志)

n的以2为底的log2(n)对数,即对2进行对数得到仅取n的指数,我们在log(n)时间中将1的总位数加1。

C
// C program to find total bit in given number
#include      
#include 
 
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;
}


Java
// 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


Python3
# 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


C#
// 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.


PHP


Javascript


C
/* Function to get no of bits in binary
   representation of positive integer */
#include         
    
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;
}


Java
/* 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.


Python3
# 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


C#
/* 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.


PHP
>= 1;
    }
    return $count;
}
 
// Driver Code
$i = 65;
echo(countBits($i));
 
// This code is contributed by Ajit.
?>


Javascript


输出:

7

方法2(使用位遍历)

C

/* Function to get no of bits in binary
   representation of positive integer */
#include         
    
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;
}

Java

/* 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.

Python3

# 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

C#

/* 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.

的PHP

>= 1;
    }
    return $count;
}
 
// Driver Code
$i = 65;
echo(countBits($i));
 
// This code is contributed by Ajit.
?>

Java脚本


输出:

7