Sponsored Ad

Saturday, April 10, 2010

How to Check if Number is power of Two(2)

This small utility will help you to find out if a given number is power of two or not. Just pass the given number in this utility and will return true or false.

 

private static bool IsPowerOfTwo(ulong val)
    {
        while (val > 0)
        {
            if ((val & 0x1) != 0)
            {
                // If the low bit is set, it should be the only bit set
                if (val != 0x1)
                {
                    return false;
                }
            }

            val >>= 1;
        }

        return true;
    }

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers