Calculate how many bytes is enough to hold the value. - CSharp System

CSharp examples for System:Byte

Description

Calculate how many bytes is enough to hold the value.

Demo Code


using Microsoft.Win32;
using System.Text;
using System.IO;/*from   ww w .j  av  a 2 s.  c  o  m*/
using System;

public class Main{
        /// <summary>
        /// Calculate how many bytes is enough to hold the value.
        /// </summary>
        /// <param name="value">input value.</param>
        /// <returns>bytes number.</returns>
        public static int BytePrecision(ulong value)
        {
            int i;
            for (i = 4; i > 0; --i) // 4: sizeof(ulong)
                 if ((value >> (i-1)*8)!=0)
                    break;
            return i;
        }
}

Related Tutorials