## 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
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

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

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.

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

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

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

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;
return y;
}``````

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) ) ;

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
}

}``````

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

forgot to add my name :)

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;

}``````

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

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...

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);
}

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;

}``````

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 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``````

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

THis line should be or operation not and

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

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)

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 | (m << i);
return n;
}``````

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

int main() {

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

num3 = num1 ^ num2;
mask = ((1 << j) - 1) << i;
num2 ^= num3;

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

}

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

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

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;

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

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

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;
if (start>end) {end = start; start=t; }
len = end-start-1;

}
Input:
int d = 0xffffff;
int e= 0x01;
set(&d,e,0,4);
out will be: 0xfffff3;

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

//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;

}

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;

}

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;

}

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;

}

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
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
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 }``````

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.

### 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.