星期三, 10月 15, 2014

[C#] How do you check if a number is a power of 2?

MVA Twenty C# Questions Explained - [07 ​How do you check if a number is a power of 2?]

很特別的主題
namespace MVATwentyQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            bool result = false;
            result = CheckPowerOfTwo(8);
            Console.WriteLine(result.ToString());
        }

        static bool CheckPowerOfTwo(ulong number)
        {
            return (number != 0) && ((number & (number - 1)) == 0);
        }

    }
}

沒有留言:

張貼留言