NVIDIA Interview Question
Software Engineer / DevelopersCountry: United States
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);
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;
}
// 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;
}
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;
}
#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;
}
//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
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;
}
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;
//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));
}
//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));
}
//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));
}
//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));
}
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 }
int replace_bits(int a, int b, int x, int y)
- axa May 08, 2012{
int mask = ((1 << (y - x + 1)) - 1) << x;
// Clear a and replace with that of b
return ((a & ~mask) | (b & mask));
}