## NVIDIA Interview Question for Software Engineer / Developers

• 0

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

well ths soultion will be like
1) let the number be A = 10101111
2) A1 = A & 10101010 = 10101010
3) A2 = A & 01010101 = 00000101
4) A1 = A1 >> 1 = 01010101
5) A2 = A2 << 1 = 00001010
6) Answer = A1 | A2 = 01011111

so u require 5 instructions

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

XOR with 1's

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

XOR with 1's will not work. If num=11011010, XOR will give 00100101 instead of 11100101.

My solution is:

int reverseOddEvenBits (int src)
{
int rst;

/* 0xaa = 10101010, 0x55 = 01010101 */
rst = ((src & 0xaa) » 1) | ((src & 0x55) « 1);

return rst;

}

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

need only one ~ (not) operation.
Eg. A= 0x10101111;
B = ~A = 0x01010000;

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

We need to swap even and odd bits .
for A=0x10101111;
Ans should be B= 0x01011111;

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

here we r dng either rotate left with carrry or rotate with right
so if we consider rotate left
a1=num & 0x80000;//get the msb
a=a<<1; //where a is number //on which we r apllying
a=a |a1;
so we required here three instructions

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

not operator would not work in this case.

for example,

A=00000000
~A = 11111111

where swap(A) should return 00000000

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

I think it needs 3 instructions:
shl \$0x1, \$A
jnc 1f
or 0x1, \$A
1f:

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

Can you elaborate with an example?

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

e.g A = 10101010

What the instruction "shl \$0x1, \$A" does is as follows:
CF A
1 <-- 01010100 <-- 0

Now, CFlag is 1.

The logic of the rest 2 instructions is as follows:
If CF is 0, does nothing; Otherwise, set the last bit of A to 1.
In another word, these two instuctions set the last bit of A as the value of CF.

In this example, A will be set as 01010101

Another example: A = 01010111
The first instruction does:
CF A
0 <-- 10101110 <-- 0

Because CF is 0, it will do nothing. The result is already what we want.

Similarly, you can use the instruction "SHR", too.

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

Sorry that there are two typos in the codes. The correct one should be:

shl \$0x1, \$A
jnc 1f
or \$0x1, \$A
1:

And it is gcc assembler syntax. For the intel assembler syntax, it should be
shl A, 1
jnc label1
or A, 1
label1:

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

there is problem in the piece of code u've posted

say my number is: num = 00111001(binary) which after swapping odd n even bits shud be 00110110

shl num,1 wud make it 01110010
and as there is no carry generated this wud be the result generated and that is wrong.

swapping odd n even bits wud take 5 instuctions atleast. as posted by deepak sharma

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

However, if you write deepak shrma's code in a asembly language, it will take 6 instructions instead of 5.

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

You are right. I am cofused.

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

int swapBits(unsigned x)
{
return ((x & 0xAAAAAAAA)>>1) | ((x<<0x55555555))
}

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

well you mean
int swapBits(unsigned x)
{
return ( ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1) );
}

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

5 makes sense

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

Isn't this just a barrel shifter?

// n is the thing you want to change

result = (n << 1) | ((n >> 31) & 1)

e.g.
int n = 1110 00...00 0101
n << 1 = 1100 00...00 1010
((n >> 31) & 1) = 1
result = 1100 00...00 1011

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

int main()
{
char temp;
char *str=(char *)malloc(100);
printf("Enter Number in Binary form");
gets(str);
for(int i=0;i<strlen(str);i=i+2)
{
temp=*(str+i);
*(str+i)=*(str+i+1);
*(str+i+1)=temp;
}
printf("Output is");
printf("%s",str);
getch();
}

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.