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

- ashot madatyan May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

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

- ashot madatyan May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explan me....what is happening here...
((ch & 0xAA)>>1 ) | ( (ch & 0x55) << 1)
Please explain in details
Thanks a ton in Advance

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

@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.
Hope that answered your question.

- ashot madatyan May 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code is platform dependent.

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

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

- Pranav August 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why shift 1 left and right?

- wcare October 11, 2012 | Flag
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

- I Want to learn May 16, 2012 | Flag Reply
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.

flip_adj (int value)
{
    int i, mask, temp;

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

        temp = value;
        temp = temp & mask;

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

        temp = temp & mask;
        value = value & (~mask);
        value = value | temp;
   }

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

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

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

    flip_adj(value);
}

Running program:

# Value = 0xaa (1010 1010)

# After flip = 0x55 (0101 0101)

- fender bender June 18, 2012 | Flag Reply
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 )
{
int i,k,andmask;
for(i=7;i>=0;i--)
{
andmask=1<<i;
k=ch&andmask;
k==0?printf("0"):printf("1");
}
}

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

- KUNAL July 27, 2012 | Flag Reply
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;
}

- bkharpuse November 08, 2012 | Flag Reply
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

- JasonBoy February 05, 2013 | Flag Reply
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];
}

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

mask1=0XFF;
             mask2=0XFF00;
             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

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

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

- ashot madatyan May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah My Bad
sorry i didnt pay attention while reading the q

- salvo4u May 16, 2012 | Flag


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