C# codes as below:
static int GetNum(int value)
{
if (value == 0)
return 0;
int max=32;
int min = 0;
int actual=0;
int mark = 1;
if (value < 0)
{
mark = -1;
value*=-1;
}
while (max > min)
{
actual = (max + min) / 2;
if (value < 1 << actual)
{
max = actual;
}
else
{
if (max - min == 1)
break;
min = actual;
}
}
return mark * 1 << ((max + min) / 2);
}