Докажите, что двоичное представление числа n>=1 содержит [lg2n]+1 битов - C#
Формулировка задачи:
Докажите, что двоичное представление числа n>=1 содержит [lg2n]+1 битов
Решение задачи: «Докажите, что двоичное представление числа n>=1 содержит [lg2n]+1 битов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(CountBits(16));
Console.WriteLine(TheoryCountBits(16));
Console.ReadKey();
}
static double TheoryCountBits(int val)
{
return Math.Log(val, 2) + 1;
}
static int CountBits(int val) {
int count = 0;
while (val != 0) { val = val >> 1; count++; }
return count;
}
}
}