Qualcomm Interview Question for Software Engineer / Developers






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

int ReverseBits( int num )
{
int bitLength= 8 * sizeof(int) ;
int outNum = 0 ;
for ( int i = 0 ; i < bitLength ; i++ )
{
y |= x & 1 ;
x >>= 1 ;
y <<= 1;
}
return y;
} // end function

- alok kumar September 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Alok.... What is x and y? Is it num and outnum?

- AD April 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 0 vote

int i = 8;
int a=0;
while(i>0)
{
a = a+(i&1);
i=i>>1;
if(i>0)
a<<=1;
}
cout<<"final->"<<a<<endl;

- goku October 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int one = 1;
int y = 0;
int x;

int temp;
int count = 0;

printf("\nenter value:");
scanf("%d",&x);

temp = x;
while(temp != 0)
{
temp = temp>>1;
count++;
}

one = one<<count;

printf("\n one = %d",one);

printf("\n count = %d",count);
one = 1;

while(count > 0)
{
y = y|((x & one)<<(count-1));
x = x>>1;
count--;
}

printf("\nreverse number = %d",y);

- sush December 26, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR wont reverse the numbers, it will just flip the bits from 1s to 0 and vice versa.

u could do this very simply by using a loop and ANDing 1 bit at a time and getting it done!

- siva March 15, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<8;i++)
{
y |= (((x>>i)&0x100) >>(i+1))| (((x>>i)&1)<<(15-i));

}

- siva March 15, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//consider int is 2 bytes for simplicity
//A is the input integer

//to swap adjacent bytes
A = ( (A & 0x00FF)<<8 ) | ( (A & ~0x00FF)>>8 )

//to swap adjacent nibbles within each byte
A = ( (A & 0x0F0F)<<4 ) | ( (A & ~0x0F0F)>>4 )

//to swap 2 bits within each nibble
A = ( (A & 0x3333)<<2 ) | ( (A & ~0x3333)>>2 )

//to swap 2 adjacent bits
A = ( (A & 0x5555)<<1 ) | ( (A & ~0x5555)>>1 )

//Note: the code is not machine independent.

- Naveen M R March 16, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
A is the input integer
A = ( (A & 0x00FF)<<8 ) | ( (A & ~0x00FF)>>8 )
A = ( (A & 0x0F0F)<<4 ) | ( (A & ~0x0F0F)>>4 )
A = ( (A & 0x3333)<<2 ) | ( (A & ~0x3333)>>2 )
A = ( (A & 0x5555)<<1 ) | ( (A & ~0x5555)>>1 )

This solution uses the above logic but this is machine independent.
Assumption:
A is the input integer & all the variables are declared.
*/

//start intializing the local variables
no_of_bits = 8 * sizeof(int);
shift_value = no_of_bits/2;
flag = 1;
j=0;
i=0;
y=0;
//end intializing the local variables

for(;shift_value;shift_value = shift_value / 2)
{
//This loop is to calculate the Mask value
while(i < no_of_bits - 1)
{
if(flag)
{
while(j < shift_value)
{
y = power(2,i); //to set the 'i'th bit of 'y'
Mask = Mask | y;
i++;
j++;
y=0;
}
}
else
{
while(j<shift_value )
{
i++;
j++;
}
}

//Toggling flag value for next run
flag = flag ? 0 : 1;
j=0;
}

A = ( (A & Mask)<<shift_value) | ( (A & ~Mask)>>shift_value);

Mask = 0;
//reset Mask to zero for next run

}

- Naveen M R March 16, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{
     int i,no,temp=0,count=0;
     printf("\nEnter the Number :");
	 scanf("%d",&no);
	 temp=no;
	 while(temp!=0)
	 {
		 temp=temp>>1; 
		 count++;
	 }
     temp=0;
	 for(i=0;i<count;i++)
	 {
	     temp= (temp<<1) + (no%2);
		 no=no>>1; 
	 }
     printf("AFTER REVERSE BITS : - %d", temp) ;
}

- Kishore N. June 20, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverse(int num)
{
int result = 0,count = 0;
while(count++ < 32)
{
result<<=1;
result|=(num&0x1);
num>>=1;
}
return result;
}

- Sanjay December 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int i = in ;
int out = 0;
while(i!=0)
{

int j = i & 1;
i = i >> 1;
std::cout << j << " ";
out = out << 1;
out = out | j ;
}

std::cout << "result --> " << out;

- Ripul March 13, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverseInt2()
{
int iNum = 32771, iRes = 0;
while(iNum)
{
iRes = iRes << 1;
iRes |= iNum & 0x01;
iNum = iNum >> 1;
}

return iRes;
}

- TY - mot April 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int reverseInt(unsigned int in)
{
int length = 8*sizeof(int);
int ret = 0;

while(length>0) {
ret |= (in>>length & 1)<<(31-length);
length--;
}
return ret;
}

- davve April 23, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

no one has posted solution using recursion... here is my try... correct me if it doesn't work....
The idea I am using is that take the last bit of the num and put it in temp. Then multiply with with pow(2,8) and add to reverse variable. basically you just pop bit from right and set them in new variable with MSB first...

ReverseBit(int num)
{
    if(!num)
        return;
    else
    {
        temp = num & 0x1;
        reverse+=temp*pow(2,BitLength);//BitLenght is dependent on the number of bits the interger is considered. For Ex: If 8 bits then BitLength = 8
        num=num>>1;
        ReverseBit(num);
     }
}

- Mayank Verma May 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
forgot to add bit length -- {{{ ReverseBit(int num) { if(!num) return; else { temp = num & 0x1; reverse+=temp*pow(2,BitLength);//BitLenght is dependent on the number of bits the interger is considered. For Ex: If 8 bits then BitLength = 8 num=num>>1; BitLength--; ReverseBit(num); } } }}{ - Mayank Verma May 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned bitRev(unsigned int num)
{
    size_t size = sizeof(num);

    unsigned j = size*8 , i = 0;
    unsigned iChooser = 1;
    unsigned jChooser = 1 << (j-1);


    for(; i<j ;i++,j--){
        int iNum = num & iChooser;
        int jNum = num & jChooser;

        iNum ? num |= jChooser : num &= ~jChooser;
        jNum ? num |= iChooser : num &= ~iChooser;

        iChooser = iChooser<<1;
        jChooser = jChooser>>1;
    }

    return num;
}

- Anonymous May 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is memory efficient than the previous one

unsigned bitRev(unsigned int num)
{
    size_t size = sizeof(num);

    unsigned j = size*8 , i = 0;

    for(; i<j ;i++,j--){
        int iNum = (num >> i) & 0x1;
        int jNum = (num >> (j-1) ) & 0x1;

        iNum ? num |= (1 << (j-1) ) : num &= ~(1 << (j-1) );
        jNum ? num |= (1 << i) : num &= ~(1 << i);
    }

    return num;
}

- Anonymous May 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(int num) {
int origNum = num;
int reversed = 0;
int numBits = 0;

//count number of bits in the given number (actually the result in numBits will be one less)
while ((num >>= 1) > 0) numBits++;

while (numBits >= 0) {
reversed += (1<<numBits)*(origNum & 1);
origNum >>= 1;
numBits -= 1;
}
printf("\nReversed Num: %d\n", reversed);
}

- Anonymous January 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//x is the int to reverse, bitsize is the bit size of int e.g. (8*4) for int
unsigned int temp = 0;

for( int i=0; i < bitsize; i++ )
{
temp = x & 0x1; //record first LSB
x >>= 1;
x |= (temp<<(bitsize-1));
i++;
}

- Anonymous April 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lookup table is a better solution.

- Ian F. April 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

XOR with all 1s

- shawshank January 26, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi shawshank

How will XOR help in reversing the bit representation of an integer. Can you please show with an example, say if number is 10110001 in binary form. How does 10110001 XOR 11111111 get u reverse bit pattern?

Thanks
Ravi

- Ravi March 10, 2007 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

10110001 XOR 11111111 = 01001110

XOR is logic high (1) when only input is high. It will produce a logic low (0) output is all inputs are either all high (1) or all low (1).

So, if you take something and XOR it with one, here are the possibilities:

0 XOR 1 = 1
1 XOR 1 = 0

As you can see, you have inverted the bits.

- MC February 13, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question is about 'reversing the bit represenation' & not about inverting them.
XOR won't work here.

- Heramb July 10, 2013 | Flag


Add a Comment
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.

Learn More

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.

Learn More

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.

Learn More

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.

Learn More