NVIDIA Interview Question






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

int modifyInt(int n,int a, int b, int k)
{


}

- Anonymous November 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int modifyInt(int n, int a ,int b, int k )
{

int temp1,temp2;
temp1=n;
n=n>>(b+1); /*right shift the number by b+1 bits
n=n <<(b+1);
/* left shift the above result by b+1 bits - this makes the last b+1 bits of n zero */
k=k >> a;
/*now right shift k by 'a' bits */
temp2=temp1 << (8*sizeof(int)-a);
temp2=temp2 >>(8*sizeof(int)-a);
/*temp2 now holds the value of the last 'a' bits of n */
temp1=temp1 | k;
/*this replaces the b+1 bits of n with k - now we need to shift this by a bits and concatenate with a to retain the original 'a' bits */
temp1= temp1 << a;
temp2=temp2 | temp1;
return(temp2);
}

- Pallavi November 28, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int modifyInt(int n, int a ,int b, int k )
{

int temp1,temp2;
temp1=Oxffffffff;
temp1=temp1>>(a); /*right shift the number by a bits
temp1=temp1<<(a);
/* left shift the above result by a bits - this makes the last
a bits of n zero */
temp1=temp1<<(8*sizeof(int)-b);
temp1=temp1>>(8*sizeof(int)-b);
temp1=NOT(temp1);//invert the 1's and 0's
n=n&&temp1;
/*now right shift k by 'a' bits */
temp2=k << (a);
n=n||temp2;
return(n);
}

- amm February 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

Hey,
if N=1011 K=0100 A=1 B=2

So we need to move bits 1 & 2 from K to N. So N becomes 1101

I did something like this

pattern = ~(~0<<(B-A+1))<<A;
N= (N& ~pattern)|( K & pattern);

First statement gives mask from location B to A.
Second just copies required bits from K to N.

For Ex. A=2 ,B=3
~0<<(B-A+1) = 11111100
~(~0<<(B-A+1)) = 00000011
Pattern = ~(~0<<(B-A+1))<<A = 00001100

1.And pattern with K then we get bits needs to be copied .
2. Use the same pattern to remove same bits from N (N &~ pattern).
or above two statements to get result.

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

i/p = n;//A and B start from 0
o/p = (1<<A)|((n|((((1<<B)-1)*2+1)^((1<<A)-1)))&(((((1<<B)-1)*2+1)^((1<<A)-1))^~0));

- mail2vcp@gmail.com October 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i/p = n;
o/p = n|((n&~(((1<<A)-1)^(((1<<B)*2)+1)))&(k<<A));

- mail2vcp@gmail.com October 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

mask = (1<<B-A+1) - 1; // make the mask
N |= mask << A; // set all bits to 1 between A and B
N &= k << A; // set all bits between A and B to K

- redroof November 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i,j bit positions

int i=2;
int j=3;
int stripe=j-i+1;
unsigned int L= (N>>(j+1)) ;
N=(((L<<stripe)|M)<<i) | (((N>>i)<<i)^N);

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

#include <stdio.h>
int main() {
    unsigned num = 8;
    unsigned int a = 4;
    unsigned int b = 7;
    unsigned int k = 5;
     unsigned int mask;
     int r =0;
    mask = ((unsigned int)~0<<a) & ((unsigned int)~0>>(31-b));
    k = ((unsigned int)k<<a);
    printf("%d\n",k);
   // printf("%d %d\n",k,mask);
    r = (num & ~mask) | k;
    printf("%x",r);

- geeksystems June 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int updatebits(int n, int m ,int i , int j){
    //create mask
    int allones =~0;
    int left = allones<<j;
    int right = (1<<i) -1;
    int mask = left|right;
    
    int n_cleared = n&mask;
    int shifted_m = m<<i;
    return n_cleared|shifted_m;
}

- Sarthak Bansal October 27, 2017 | Flag Reply


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