NVIDIA Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

This is basically swapping odd and even bits, which is done using bitmasks. Implementation provided below:

``````unsigned char swap_bits(unsigned char ch)
{
return ((ch & 0xAA)>>1 ) | ( (ch & 0x55) << 1);
}``````

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

it should be (ch & 0x55) on the right side....

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

That's correct, thanks for noting that. Already fixed.

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

Could you explan me....what is happening here...
((ch & 0xAA)>>1 ) | ( (ch & 0x55) << 1)

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

@Raj: Use a binary calculator to see what the 0x55 and 0xAA look like in binary.
1. Mask out all the even bits in the char: ch & 0xAA
2. Mask out all the odd bits in the char: ch & 0x55
Now you have two values - the masked odd and even bits of the original number. So, all you have to do now is to use a biwise OR operation to combine these two values, having shifted by 1 position to the left and to the right the respective temp numbers.

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

The code is platform dependent.

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

@TheWofle :The dependency is just due to the size of integer.In gcc its 32 bit so above mentioned code will not work on it it will swap only 8 bits. So for gcc you can modify code as
return ( ((ch & 0xaaaaaaaa) >> 1) | ((ch & 0x55555555) << 1) );

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

Why shift 1 left and right?

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

``````#include<stdio.h>

int main()
{

int n=10;
int m;
m=n;
int count=0;
while(m)
{
count++;
m=m>>1;
}
printf("Value before swapping:%d\n",n);
printf("No of Bits:%d",count);
int i=0;
int k=1;
while(k<=count/2)
{
int a=1;
int b=1;

a=((a<<i)&n)>>i;
b=((b<<(i+1))&n)>>(i+1);
//a=a&n;
//b=b&n;

//a=a>>i;
//b=b>>(i+1);

int c=a^b;
int d=(c<<i)|(c<<(i+1));

n=n^d;
i=i+2;
k++;
}
printf("\nValue after Swapping Bits:%d",n);

return 0;
}``````

Output:

Value before swapping:10(1010)
No of Bits:4
Value after Swapping Bits:5(0101)

----------------------------------
Value before swapping:21(10101
No of Bits:5
Value after Swapping Bits:26(11010)
---------------------------------
Value before swapping:15(1111)
No of Bits:4
Value after Swapping Bits:15(1111)

Done for integer
May be this can be true for integer also.
Replacing char n='b';and int m with char m;gives following output.
Value before swapping:98 b
No of Bits:7
Value after Swapping Bits:81 Q

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

``````A little long, but works generically for larger sizes. Mask is constructed dynamically.

{

for (i = 0; i < (sizeof(int)*8); i = i+2) {

temp = value;

if (((temp >> i) & 1) != ((temp >> (i+1)) & 1)) {
temp = ~temp;
}

value = value | temp;
}

printf("\nAfter flip = %#x", value);
}

main ()
{
int value = 0xaa; // 1010 1010

printf("\nValue = %#x", value);

}

Running program:

# Value = 0xaa (1010 1010)

# After flip = 0x55 (0101 0101)``````

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

void showbits(char);
void swapbits(char,int,int);
void main()
{
int i,j;
char ch='A';
printf("\n input the character\n");
scanf("%c",&ch);
printf("character is %c and its binary equivalent is \n",ch);
showbits(ch);
printf("\n After swaping..");
for(j=7;j>=0;)
{
swapbits(ch,j,j-1);
j=j-2;
}
printf("\n");
getch();
}

void showbits(char ch )
{
for(i=7;i>=0;i--)
{
k==0?printf("0"):printf("1");
}
}

void swapbits(char ch,int j,int l )
{
k==0?printf("0"):printf("1");
k==0?printf("0"):printf("1");
}

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

#include<stdio.h>
#include<conio.h>
int binary(int);
int swap(int);
int in[100];
int main()
{
unsigned char c;
printf("enter any character=");
scanf("%c",&c);
int d,i,count;
d = (int)c;
printf("%d\n",d);
count = binary(d);
for(i = count;i>=0;i--)
printf("%d",in[i]);
swap(count);
printf("\n\n");
for(i = count;i>=0;i--)
printf("%d",in[i]);
getch();
return 0;
}

int binary(int num)
{
int j=0;
while(num!=1)
{

in[j] = num%2;
num = num/2;
j++;
}
in[j] = 1;
return j;
}
int swap(int a)
{
int i,j;
for(i=a;i>=0;i--)
{
if(i==0)
{}
else{
j = in[i];
in[i] = in[i-1];
in[i-1] = j;
i--;
}}

return 0;
}

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

let the number be A = 10101111
A1 = A & 10101010 = 10101010
A2 = A & 01010101 = 00000101
A1 = A1 >> 1 = 01010101
A2 = A2 << 1 = 00001010
result= A1 | A2 = 01011111

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

unsigned char swap_tbl[3] = {0, 2, 1, 3};

unsigned char swap(unsigned char c)
{
return swap_tbl[(c & 0xc0)>>6] << 6 | swap_tbl[(c & 0x30) >> 4] << 4 | swap_tbl[(c & 0xc) >> 2] << 2 | swap_tbl[c & 0x3];
}

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

``````mask1=0XFF;
int byte1 = (number & mask1)<<8;
int byte2 = (number & mask2)>>8;
int newumber = byte1 + byte2;``````

* You can extend the above logic to extend to the whole unsigned number .
* Just generate the masks accordingly

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

@salvo: This does not do what is required - swap every two bits. Instead, it does two lower BYTES.

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

sorry i didnt pay attention while reading the q

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.