Qualcomm Interview Question Software Engineer in Tests

• 0

If a number is power of 2

Comment hidden because of low score. Click to expand.
1
of 1 vote

for unsigned integer nummbers:
bool isPow2(int n)
{
return !(n & (n-1)) && n;
}

Comment hidden because of low score. Click to expand.
1
of 1 vote

if number of bits set is 1 then it is a power of two

Comment hidden because of low score. Click to expand.
0
of 0 vote

for unsigned integer numbers:
bool isPow2(int n)
{
return !(n & (n-1)) && n;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

return num&(num-1)==0

Comment hidden because of low score. Click to expand.
0

can you explain it?

Comment hidden because of low score. Click to expand.
0

let us take num=8 (2 pow 3) and n-1 means 7
n =1000
n-1=0111 perform & operation
----------------
0000
------------- so 8 is power of 2

Comment hidden because of low score. Click to expand.
0
of 0 vote

return ((pow(2,log2(n))-pow(2,floor(log2(n))))==0)

Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPow2(unsigned int n){
if(n==0)
return false;
return (n&(n-1))==0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

If(x && !(x & (x-1))==0)

Comment hidden because of low score. Click to expand.
0
of 0 vote

@Jay: Thats interesting point. I was wondering if Negative numbers can be powers of positive number!

Comment hidden because of low score. Click to expand.
0
of 0 vote

bool ispowerof2(int x)
{
int p = x & -x;
return p == x || p == -x;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

powers of a +ve number 2 will always be +ve

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
bool val ( int n);
int main ()
{
if(val(1024) == true)
cout << "yes";
else
cout <<"no";

}

bool val(int n)
{

if( n <= 1)
return false;

while(n>1)
{
if(n%2 != 0)
return false;
else
n = n / 2;
}

return true;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
}
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

What about negative number? such as -4. -4 is power of 2 too.

Comment hidden because of low score. Click to expand.
0

For negative numbers add this line:

bool isPow2(int n){
n=n<0?-n:n;
return !(n & (n-1));
}

Comment hidden because of low score. Click to expand.
0

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.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.