## Qualcomm Interview Question for Software Engineer in Tests

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.
1
of 1 vote

(num & (num-1))?notpower:power;

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

x^x-1 if all bits are 1 then power of 2 else not.

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

x & x-1. Check if output is zero.
This is easy to check than checking all bits are one in case of x^x-1

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

if n&(n-1)== 0, then number is power of 2.

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

it will not work if value is 0, correct method is

f = v && !(v & (v - 1));

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

squre root of another number

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

if only the left most bit is 1 others 0 only then it is power of 2.. eg..1000 or 0010
x>>1 if 0 continue
else if 1 then the next bit should not be 1.

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

take log of that number to the base 2 if the result is an integer then it is a power of 2 else if it is a float it is not a power of 2

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

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

``````int x = 31;
int max = 0;
String s = Integer.toBinaryString(x);
for(int i=0; i<s.length(); i++){
max = (int) (max + (Math.pow(2, i)));
}
int k = Math.abs((~x & max)+1);
if(x == k){
System.out.println("power of 2");
}
else{
System.out.println("not power of 2");
}``````

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

int isNonPowerOf2(num)
{
Power=0;
if (num=0) reutrn 0;

while(num>0)
{
power+=(num&0x1);
num = num>>1;
}
return power-1;
}

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

int isNonPowerOf2(num)

``````Power=0;
if (num=0) reutrn 0;

while(num>0)
{
power+=(num&0x1);
num = num>>1;
}
return power-1;``````

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

Just check the last bit of given number, if it is set, its not the power of 2

Bool isPowerof2 = ((A & 0x1) >0) ? (FALSE) : (TRUE);

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

Only last bits value is 1, all other bits of power of 2 like

6 = 0110 = (2^3)*0 + (2^2)*1+ (2^1)*1+ (2^0)*0

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

2's complement of the of the number and number itself will be equal when the number is the power of two..

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.
0
of 0 vote

/* function to find no is power of 2 or not */

void find(int n)
{
if (n &(n-1))
printf ("Number is not power of 2\n");
else
printf ("Number is Power of 2\n");
}

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

Count the Number of 1(bits) in the number. only if count is 1, then the number is power of 2. how ever ignore least significant bit

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

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

bool powerof2(int x)
{
unsigned int i = 0;
int count = 0;
bool p2 = true;
if (x == 1)
{
return false;
}
else
{
while (i < 31)
{
if (x & 1 << i)
{
++count;
if (count > 1)
{
p2 = false;
break;
}
}
i++;
}
}
return p2;
}

int _tmain(int argc, _TCHAR* argv[])
{
bool p2 = powerof2(32);
return 0;
}

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

#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

bool powerof2(int x)
{
unsigned int i = 0;
int count = 0;
bool p2 = true;
if (x == 1)
{
return false;
}
else
{
while (i < 31)
{
if (x & 1 << i)
{
++count;
//// If Count > 1 it is not a power of 2
if (count > 1)
{
p2 = false;
break;
}
}
i++;
}
}
return p2;
}

int _tmain(int argc, _TCHAR* argv[])
{
bool p2 = powerof2(32);
return 0;
}

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 on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### 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.

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.