Qualcomm Interview Question
Software Engineer / Developersint i = 8;
int a=0;
while(i>0)
{
a = a+(i&1);
i=i>>1;
if(i>0)
a<<=1;
}
cout<<"final->"<<a<<endl;
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);
//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.
/*
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
}
int reverse(int num)
{
int result = 0,count = 0;
while(count++ < 32)
{
result<<=1;
result|=(num&0x1);
num>>=1;
}
return result;
}
int reverseInt2()
{
int iNum = 32771, iRes = 0;
while(iNum)
{
iRes = iRes << 1;
iRes |= iNum & 0x01;
iNum = iNum >> 1;
}
return iRes;
}
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);
}
}
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;
}
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;
}
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);
}
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
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.
int ReverseBits( int num )
- alok kumar September 28, 2007{
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