NVIDIA Interview Question for Software Engineer / Developers


Country: United States




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

int replace_bits(int a, int b, int x, int y)
{
int mask = ((1 << (y - x + 1)) - 1) << x;
// Clear a and replace with that of b
return ((a & ~mask) | (b & mask));
}

- axa May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How about
int mask = (1<<n1)&(1<<n2);
return ((a&~mask)|(b&mask))

- Omii May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct. mask is always 0 when n1 != n2

- axa May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I do not believe axa's solution is not correct, because b needs to be shifted by x before &, e.g.

return ((a & ~mask) | ((b << x) & mask);

- coolio July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone please explain what's going on? How the mask is created?

- Wcare October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question is definitely not formatted well.Actual question is: Write a function to set 'm' bits of integer 'X' in between two bit positions of integer 'Y'.

/* 
 *(pos_b-pos_a) bits starting from 0 bit of 'y'
 * is being set between pos_a and pos_b of x.
 */
int replace_bits(int pos_a, int pos_b, int x, int y)
{
        /* pos_a is the lowest position i.e. LSB*/
        int total_bits = pos_b - pos_a;
        /*
         * right shifting is basically multiplying
         * and then subtracting by 1
         */
        int mask = (2 << total_bits) - 1;
        printf("mask %d\n", mask);
        x = (x&~((mask)<<pos_a)) | (y&mask);
        return y;
}

- aka November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

s => start bit
e => end bit
first => first value
second => second value

Now

bits = e - s + 1;
mask =( (~0) << ((sizeof(int) * 8) - bits) ) >> ((sizeof(int) * 8) - bits - s) ) ;
(first & ~mask) | (second & mask)

- black March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// let the integers be m and n, let the bit positions be x, y
  // let m = 24 , n = 15 , x = 2 , y = 4 
   
   int to_to_anded = pow(2, x - 1)  // for x = 1, generates 1, for 2, 2, for 3, 4
   int i;
   for ( i = x; i <= y; i++) {
         int res = n & to_be_anded ; // get the ith bit value of n
         if (res == 0) {
                // set the ith bit of m to 0
                m = m & ! to_be_added; 
         } else { // set to 1
                m = m | to_be_added;  
        }
        to_be_added = to_be_added << 1;

   }

- Anonymous May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to add my name :)

- Mukesh May 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

n and m are the given numbers; i and j are the indices with i>j; the bits in n between i and j are replaced by m.

private static void updateBits(int n, int m, int i, int j) {
			
	int max = ~0;
	
	int a = (1 << (j+1)) - 1;
	
	int b = max - a;
	
	int c = (1 << i) - 1;
	
	int mask = b | c;
	
	int first = n & mask;
	
	int second = m << i;
	
	int soln = first | second;
		
	}

- chaitea May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One problem I can see with this solution is it doesn't account for the case when m has more bits than j-i+1, e.g., overflow. It's probably best to also & second with the mask.

Also int should probably be unsigned int...

- coolio July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Swap(uInt Val1, uInt Val2, uInt Pst1, uInt Pst2)
{
Val1 = (((Val1 >> Pst2) << Pst2) | ((Val1 << (32 - Pst1)) >> (32 - Pst1)));
Val2 = ((((Val2 << (32 - Pst2)) >> (32 - Pst2)) >> Pst1) << Pst1);
Val1 = (Val1 | Val2);
}

- Nivas September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

int main()
{
    unsigned int N = 1024;
    unsigned int M = 21;

    unsigned int i = 2, j = 6;

    unsigned int temp = ~0;

    unsigned int left = temp >> (32 - i);
    std::cout << "left: " << left << std::endl;
    unsigned int right = temp << (j + 1);
    std::cout << "right: " << right << std::endl;

    unsigned int res = (N & (left | right)) | (M << i);
    std::cout << "result: " << res << std::endl;

}

- hao.liu0708 September 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//based on 16 bit
int v1 = 1010 1010 1010 1010;
int v2 = 1010 1111 1011 1010;
int i = 10, int j = 3; // both inclusive

int mask = ~0; // 1111 1111 1111 1111
mask = (mask >> i) << i; // 1111 1100 0000 0000 ;

int m1 = (~0) >> (16 - j +1); // 11;

int mask2 = mask & m1; // 1111 1100 0000 0011


int m3 = ~mask2; // 11 1111 1100;
int m4 = v2 & m3; // 11 1011 1000

int result = (mask2 & v1) | (m4) ; // 1010 1000 0000 0010 | 11 1011 1000 = 1010 1011 1011 1010

- Vipul October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

THis line should be or operation not and

int mask2 = mask | m1; // 1111 1100 0000 0011

- vipul4group October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

partial= A&(~0<<i) | (A &(1<<j - 1))
Result = partial | (B<<j)

- JasonBoy February 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int insert(int m, int n, int i, int j)
{
    int temp = j - i + 1;
    int temp1 = (1 << temp) - 1;
    int mask = ~(temp1 << i);
    n = n & mask;
    n = n | (m << i);
    return n;
}

- Ashish19121 March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main() {

int num1 = 0xE3, num2 = 0xF2, num3 = 0;
int mask = 0;
int i = 2, j= 4;

num3 = num1 ^ num2;
mask = ((1 << j) - 1) << i;
printf("mask = %d\n", mask);
num3 &= mask;
num2 ^= num3;

printf("%d.\n", num2);
return 0;

}

- prem April 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Solution with xor. have broken down for better understanding.. and making it readable..

- prem April 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i & j are integers, & n1 & n2 are bit positions ( n1 > n2)

int n = pow(2,(n1-n2)) -1 ;
   int mask = n << n2;
   i = ( j & ~mask) | ( i & mask);

- Manish September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(a >> p2 & ((1 << p1) -1)) << p2

- Anonymous August 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void set(int * a, int b , int start, int end)
{
int t = end;
int len;
int mask;
if (start>end) {end = start; start=t; }
len = end-start-1;
mask = ((1<<len)-1);

*a= ((*a)& ~(mask<< (start+1))) |((b&mask) << (start +1));
}
Input:
int d = 0xffffff;
int e= 0x01;
set(&d,e,0,4);
out will be: 0xfffff3;

- vathsala September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

//reaplcebits: takes bits between pos1 and pos2 of input a and put it input b between the
//same positions.
void replacebits(unsigned int a , unsigned int* b,unsigned int pos1, unsigned int pos2)

{
int unsigned tmp , mask , bm;
printf("a==%x b===%x pos1==%x pos2===%x",a,(*b),pos1,pos2);

//swap pos1 and pos2 if pos1 < pos2

if(pos1<pos2){
tmp=pos2;
pos2=pos1;
pos1=tmp;
}

//number of bits in a mask
bm=pos1-pos2-1;

//prepared mask
mask= ((1<<bm) -1) << (pos1-bm);
printf("mask==%x",mask);

*b = ((a&mask)|(*b&~mask));


}

- nagaraju February 17, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//reaplcebits: takes bits between pos1 and pos2 of input a and put it input b between the
//same positions.
unsigned int replacebits(unsigned int a , unsigned int b,unsigned int pos1, unsigned int pos2)

{
int unsigned tmp , mask , bm;
printf("a==%x b===%x pos1==%x pos2===%x",a,b,pos1,pos2);

//swap pos1 and pos2 if pos1 < pos2

if(pos1<pos2){
tmp=pos2;
pos2=pos1;
pos1=tmp;
}

//number of bits in a mask
bm=pos1-pos2-1;

//prepared mask
mask= ((1<<bm) -1) << (pos1-bm);
printf("mask==%x",mask);

return ((a&mask)|(b&~mask));

}

- vat February 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//reaplcebits: takes bits between pos1 and pos2 of input a and put it input b between the
//same positions.
void replacebits(unsigned int a , unsigned int* b,unsigned int pos1, unsigned int pos2)

{
int unsigned tmp , mask , bm;
printf("a==%x b===%x pos1==%x pos2===%x",a,(*b),pos1,pos2);

//swap pos1 and pos2 if pos1 < pos2

if(pos1<pos2){
tmp=pos2;
pos2=pos1;
pos1=tmp;
}

//number of bits in a mask
bm=pos1-pos2-1;

//prepared mask
mask= ((1<<bm) -1) << (pos1-bm);
printf("mask==%x",mask);

*b = ((a&mask)|(*b&~mask));


}

- nagaraju February 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//reaplcebits: takes bits between pos1 and pos2 of input a and put it input b between the
//same positions.
void replacebits(unsigned int a , unsigned int* b,unsigned int pos1, unsigned int pos2)

{
int unsigned tmp , mask , bm;
printf("a==%x b===%x pos1==%x pos2===%x",a,(*b),pos1,pos2);

//swap pos1 and pos2 if pos1 < pos2

if(pos1<pos2){
tmp=pos2;
pos2=pos1;
pos1=tmp;
}

//number of bits in a mask
bm=pos1-pos2-1;

//prepared mask
mask= ((1<<bm) -1) << (pos1-bm);
printf("mask==%x",mask);

*b = ((a&mask)|(*b&~mask));


}

- VathsalaNagaraju February 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 #include<stdio.h>
  2 
  3 void printBinary(int a)
  4 
  5 {
  6 
  7     unsigned int mask,bit,size;
  8 
  9     size = sizeof(int)*8;
 10 
 11     mask = 1 << size-1;
 12 
 13     for(int i =0 ;i<size;i++)
 14 
 15     {
 16 
 17         bit = (a & mask) ? 1 : 0;
 18 
 19         mask = mask >>1;
 20 
 21         printf("%d ",bit);
 22 
 23     }
 24 
 25     printf("\n");
 26 
 27 }
 28 
 29 int setBit(int pos1 , int pos2,int num1,int num2)
 30 
 31 {
 32 
 33     int n = (pos2-pos1)+1;
 34 
 35 
 36     return (num2 | (num1 &((1<<n)-1)<<(pos1-1)));
 37 
 38 }
 39 
 40 int main()
 41 
 42 {
 43 
 44     int num1,num2,pos1,pos2;
 45 
 46     printf("Enter the 1st Number :");
 47 
 48     scanf("%d",&num1);
 49 
 50     printBinary(num1);
 51 
 52     printf("Enter the 2nd Number :");
 53 
 54     scanf("%d",&num2);
 55 
 56     printBinary(num2);
 57 
 58     printf("Enter the 1st position ");
 59 
 60     scanf("%d",&pos1);
 61 
 62     printf("Enter the 2nd position :");
 63 
 64     scanf("%d",&pos2);
 65 
 66 printBinary(setBit(pos1,pos2,num1,num2));
 67 
 68 }

- sandeepkumar211221 August 17, 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