我的程序有什么问题?检查整数是否是C中的两个幂#
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool check = isPowerOfTwo(255);
Console.WriteLine(check);
Console.ReadKey();
}
public bool isPowerOfTwo (uint x)
{
while (((x % 2) == 0) && x > 1)
{
x /= 2;
}
return (x == 1);
}
}
}
我得到错误
一个对象引用是所必需的非静态字段,方法或属性。
可能值得注意的是算法本身可以改进。你应该可以使用:'return x!= 0 &&((x-1)&x)== 0' – Iridium