M
Moe Sisko
Using dotnet 2.0
I wrote some code to figure out if a number is a power of 2 :
==
private bool IsPowerOf2(int num)
{
int j = 0x1;
// test bits 0 .. 30. Don't use sign bit (bit 31).
for (int i = 0; i < 31; i++)
{
if (num == j)
return true;
j = j << 1;
}
return false;
}
==
It seems to work, but I got to wondering if there is a more efficient way to
do this. Any ideas ?
TIA
I wrote some code to figure out if a number is a power of 2 :
==
private bool IsPowerOf2(int num)
{
int j = 0x1;
// test bits 0 .. 30. Don't use sign bit (bit 31).
for (int i = 0; i < 31; i++)
{
if (num == j)
return true;
j = j << 1;
}
return false;
}
==
It seems to work, but I got to wondering if there is a more efficient way to
do this. Any ideas ?
TIA