Returns how many bits are necessary to hold a certain number - CSharp System

CSharp examples for System:Bit

Description

Returns how many bits are necessary to hold a certain number

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w  ww.  ja va2  s. co m*/

public class Main{
        /// <summary>
        /// Returns how many bits are necessary to hold a certain number
        /// </summary>
        public static int BitsToHoldUInt(uint value)
        {
            int bits = 1;
            while ((value >>= 1) != 0)
                bits++;
            return bits;
        }
}

Related Tutorials