2 की घात 2n के रूप की एक संख्या है जहां n एक पूर्णांक है
आधार के रूप में संख्या दो और घातांक के रूप में पूर्णांक n के साथ घातांक का परिणाम।
<टेबल><थेड>उदाहरण 1
class Program {
static void Main() {
Console.WriteLine(IsPowerOfTwo(9223372036854775809));
Console.WriteLine(IsPowerOfTwo(4));
Console.ReadLine();
}
static bool IsPowerOfTwo(ulong x) {
return x > 0 && (x & (x - 1)) == 0;
}
} आउटपुट
False True
उदाहरण 2
class Program {
static void Main() {
Console.WriteLine(IsPowerOfTwo(9223372036854775809));
Console.WriteLine(IsPowerOfTwo(4));
Console.ReadLine();
}
static bool IsPowerOfTwo(ulong n) {
if (n == 0)
return false;
while (n != 1) {
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
} आउटपुट
False True