2-এর ঘাত হলো 2ⁿ ফর্মের একটি নাম্বার যেখানে n একটি ইন্টিজার।
নাম্বার দুটি বেস এবং ইন্টিজার n এক্সপোনেন্ট হিসেবে এক্সপোনেনশিয়েশনের ফলাফল।
| n | 2ⁿ |
|---|---|
| 0 | 1 |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 16 |
| 5 | 32 |
উদাহরণ ১: বিটওয়াইজ অপারেশন ব্যবহার করে
উদাহরণ
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
উদাহরণ ২: লুপ ব্যবহার করে
উদাহরণ
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