How to count the total bits of a number in C#

2 Answers

0 votes
using System;

class Program
{
    public static int totalBits(int num) {
        return (int)Math.Log(num , 2) + 1; 
    }
    static void Main() {
        int n = 12; // 1100 
         
        Console.WriteLine(totalBits(n));
  
        n = 386; // 1 1000 0010
  
        Console.WriteLine(totalBits(n));
        
        n = 2658; // 1010 0110 0010
        Console.WriteLine(totalBits(n));
    }
}




/*
run:

4
9
12

*/

 



answered Dec 6, 2021 by avibootz
edited Dec 6, 2021 by avibootz
0 votes
using System;

class Program
{
    public static double totalBits(int num) {
        return Math.Floor(Math.Log(num , 2.0)) + 1; 
    }
    static void Main() {
        int n = 12; // 1100 
         
        Console.WriteLine(totalBits(n));
  
        n = 386; // 1 1000 0010
  
        Console.WriteLine(totalBits(n));
        
        n = 2658; // 1010 0110 0010
        Console.WriteLine(totalBits(n));
    }
}




/*
run:

4
9
12

*/

 



answered Dec 6, 2021 by avibootz

Related questions

1 answer 230 views
1 answer 181 views
1 answer 190 views
1 answer 137 views
...