## Qualcomm Interview Question Software Engineer in Tests

- 0of 0 votes
If a number is power of 2

static void Main(string[] args)

{

int number = enter any no;

int quotient = 0;

int mod = 0;

while(number >= 2)

{

mod = number % 2;

quotient = number / 2;

if (mod == 0 && number >= 2)

{

number = quotient; ;

continue;

}

else break;

}

if (mod == 0)

{

Console.WriteLine("number power of 2");

}

else

{

Console.WriteLine("number not power of 2");

}

Console.ReadLine();

}

For negative numbers add this line:

bool isPow2(int n){

n=n<0?-n:n;

return !(n & (n-1));

}

How? Look at the exponential graph, it never goes below 0 past the x axis.

Also, 2^x = -4, give me x. You can't. The power of any positive integer, such as 2, is always positive. Remember that a number raised to a negative number is just a positive inverse. Therefore, this case is invalid.

for unsigned integer nummbers:

- strezh on October 08, 2010 Edit | Flag Replybool isPow2(int n)

{

return !(n & (n-1)) && n;

}