Qualcomm Interview Question: Write a c functions that take... | Glassdoor

Interview Question

DSP Firmware Engineer Interview Santa Clara, CA

Write a c functions that takes two bit indices and an int

  , and reverses the bits in the int between the two indices.
Answer

Interview Answer

9 Answers

This post has been removed.
Please see our Community Guidelines or Terms of Service for more information.

0

//reverses bit between two indices of a number.
#include
using namespace std;
int main()
{
unsigned int no=90,i=2,j=6,tmp=0,tmp1=0;
tmp=no>>i;
cout>1;
}
cout>1;
if(x=i)
tmp=tmp|0;
else
tmp=tmp|0x80000000;
}
cout<<tmp<<"tmp before\n";
no=no&tmp;
no=no|(tmp1<<i);
cout<<no<<" no";
return 0;
}

noty on Apr 1, 2011
1

#include
main(int arg, char *argv[])
{
    unsigned char num, hi_indx, lo_indx, mask, num_tmp, mask_c, num_rv, iter,i;
    num = atoi(argv[1]);
    hi_indx = atoi(argv[2]);
    lo_indx = atoi(argv[3]);

    mask = ((0xFF > (7-hi_indx+lo_indx);
    mask = (mask << lo_indx);
    mask_c = ~mask;
    num_tmp = num & mask_c;
    printf("%x %d %d %x",num, hi_indx, lo_indx, mask);

    num_rv = 0x0;

    iter = hi_indx-lo_indx+1;
    i = 0;
    while(i<=iter)
    {
        int flg;
        flg=(num & (1<<(i+lo_indx)))?1:0;
        if(flg)
            num_rv=num_rv|(1<<(hi_indx-i));
        i++;
    }

    num = num_rv | num_tmp;
    printf("\nnum=%x ",num);

}

Anonymous on Nov 3, 2012
0

The above one is just to give an example with 8bits

Anonymous on Nov 4, 2012
0

#include

int toggle_bw_indices(int num, int hi_index, int lo_index)
{
    int count = 1; // assume 0th position as index 1
    int res = num;

    while (num)
    {
        if (count >= lo_index && count > 1;
        count++;
    }

    return res;
}

int main()
{
    int num = 90;
    int hi_index = 5;
    int lo_index = 2;

    printf("%d\n", toggle_bw_indices(num, hi_index, lo_index));
    return 0;
}

Suomynona on Mar 17, 2015
1

int toggle_bw_indices(int num, int hi_index, int lo_index)
{
    int rev_nib(0),rev(0);
    int mask_nib, mask;
    int nib(0);
    mask_nib = 0xF;
    mask = ~mask_nib;
    rev = (mask & num);
    nib = (mask_nib & num);
    for (int i=0; i=lo_index && i<=hi_index)
        {
            if (nib & (1<< (hi_index+lo_index-i)))
                rev_nib += 1<<i;
        }
        else
        {
            rev_nib += nib & (1<<i);
        }
    }
    rev += rev_nib;
    return rev;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int num = 90;
    int hi_index = 2; // hi and lo index can be bwteen 0 and 3 (2 bits)
    int lo_index = 0;

    printf("%d\n", toggle_bw_indices(num, hi_index, lo_index));
}

joon on May 10, 2015
0

key idea: use XOR.
int toggle_bw_indicies(int num, int hi_index, int lo_indx)
{
    int temp = 0;
    while(hi_index >= lo_index)
   {
      temp |= 1 << lo_index;
      lo_index++;
   }

   return (num ^ temp);
}

steve on Jan 5, 2016
0

int toggle_bw_indicies(int num, int i, int j)
{
    int biti = (num >> i) % 2 ; // find the value of the bit at position i
    int bitj = (num >> j) % 2 ; // find the value of the bit at position j
    int mask = -1 ^ (1>>i) ^ (1>>j) ; // set a mask which has 1 in all bits but positions i and j
    int temp = num & mask ; // set positions i and j in the original number to 0
    temp = temp | biti * (1>>j) | bitj * (1>>i) ; // move the bit value in position i to position j and vice versa
    return temp ;
}

Amirali on Mar 22, 2016
0

/**
 * Assuming that between indices is inclusive.
 * Indices range from 1=MSB to 32.
 * size of int is HW specific.
 */

#include

int main(void) {
    int number, index1, index2;
    int ii, jj, kk;
    int size_int = 8*sizeof(int);

    printf("Enter the number followed by the two indices:\n");
    scanf("%d %d %d", &number, &ii, &jj);

    // Convert the indices.
    if (ii > (index2+ii)) & 1;
            kk = (number >> (index1-ii)) & 1;
            printf("%d %d \n", jj, kk);
            // number ^= (1<<n) toggles the nth bit.
            number ^= (-jj ^ number) & (1 << index1-ii);
            number ^= (-kk ^ number) & (1 << index2+ii);
        }
        printf("%d\n", number);
    }
    return 0;
}

Anonymous on Jun 23, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.