## NVIDIA Interview Question for Software Engineer / Developers

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

Here is one way I came up with -- though will work only for unsigned numbers ..

#include <stdio.h>

void main()
{
unsigned int i = 0xABCD;
unsigned int shift = 0;
unsigned int temp = 0;
unsigned reversed = 0;
//Check all nibbles.
for( int nCount = 0; nCount < sizeof(i) * 2; nCount++ )
{
temp = i >> shift;
//just keep the nibble.
temp = temp & (0 | 0xF);
//temp is now from 0 - F.
reversed = reversed | ((0xF-temp) << shift);
shift += 4;
}
printf("Original: %x\n", i);
printf("Reversed: %x\n", reversed );
}

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

ooh god, just surf the Internet, you will find dozern of solutions. I believe this one everybody should know:

assume 32-bit number
m = 0x55555555; // 1-bit swap
x = ((x & m) << 1) | ((x & ~m) >> 1);
m = 0x33333333; // 2-bits swap
x = ((x & m) << 2) | ((x & ~m) >> 2);
m = 0x0f0f0f0f; // 4-bits swap
x = ((x & m) << 4) | ((x & ~m) >> 4);
m = 0x00ff00ff; // 8-bits swap
x = ((x & m) << 8) | ((x & ~m) >> 8);
x = (x << 16) | (x >> 16); // 16-bits swap

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

can anybody explain in detail whats exactly going on here ?

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

// v is value & r is reverse value

r = v
for (v >>= 1; v; v >>= 1)
{
r <<= 1;
r |= v & 1;
}
r <<= s; // shift when v's highest bits are zero

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

what is s in the last line?

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

uint8_t a,b; // reverse a to b
b = 0;
for(int i =0;i<8;i++)
b |= ((a>>1)&0x01) << (7-i);

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

``````int res = 0;
int rem;
int n = 0xd;
while(n)
{
rem = n%2;
n >>= 1;
res = (res<<1) | rem;``````

}

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

Will this work? (only for unsigned numbers)

``````int result = 0;
int a = 0;
while (n)
{
a = n&1;
result = result | a;
result<<1;
n>>1;
}``````

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

it should be

while (n)
{
a = n&1;
result<<=1;
result = result | a;
//result<<=1;
n>>=1;
}

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

Use this:-
Reverse_Number = Number xor 0xFFFFFFFF;

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

wouldn't this swap the individual bits?
we are supposed to "reverse" the bits, taking into consideration all 32 bits at a time, and not swap each and every bit..

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

``````unsigned int reverse_binary(int n)
{
unsigned int temp = n, result = 0;

while (temp != 0) {
if(temp & 1)
result = result | 1;

result <<= 1;
temp >>= 1;
}
result >>= 1;
return result;
}``````

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

``````public class ReverseBit {
public static void main(String args[]) {
int no = 10;
int res = 0;
int bit;
// print before reverse
printBits(no);
// go thru the int from bit 1 to 32
for (int i = 0; i < 32; i++) {
// extract bit i
bit = (no & (1 << i)) >>> i;
// push into result with shifting
res = (res << 1) | bit ;
}
// print bits after reversing
printBits(res);
}

public static void printBits(int no) {
System.out.println();
for (int i = 31; i >= 0; i--)
System.out.print((no & (1 << i)) >>> i);
}
}``````

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

unsigned char revbits = {0, 8, 4, 0xc, 2, 0xa, 6, 0xe, 1, 9, 5, 0xd, 3, 0xb, 7, 0xf};

int reversebits(int v)
{
int i = 0;
int cur = 0;
while(v)
{
i = (i << 4) | revbits[v & 0xF];
v >>= 4;
cur += 4;
}

return i << (32-cur);
}

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

Another solution using bitwise xor operation with 16 iterations is,

int reverse (int value)
{
unsigned int tmp = (unsigned int) value;
unsigned int bitcnt = 0;

while(bitcnt < 16)
{
/* Only change bits if the bit in lower 16 bits and
* corresponding bit in higher 16 bits are different
*/
if( ((tmp>>(31-bitcnt)) & 0x1) ^ ((tmp>>bitcnt) & 0x1) )
{
/* Bits differ so flip them */
tmp ^= 1<<(31-bitcnt) | 1<<bitcnt;
}
bitcnt++;
}
return((int)tmp);
}

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

I have tried the following program:

#include <stdlib.h>
#include <stdio.h>

void reverse (int );

int main()
{
int value;

printf ("\nEnter the Integer to be reversed: ");
scanf ("%d", &value);

reverse (value);
}

void reverse (int value)
{
int mask = 1 << 31;
int i;

for (i=0; i<=31; i++)
{
putchar (value & mask ? '1' : '0');
value <<= 1;
}
printf ("\nHope this helps!");
}

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

I have tried the following program:

#include <stdlib.h>
#include <stdio.h>

void reverse (int );

int main()
{
int value;

printf ("\nEnter the Integer to be reversed: ");
scanf ("%d", &value);

reverse (value);
}

void reverse (int value)
{
int mask = 1 << 31;
int i;

for (i=0; i<=31; i++)
{
putchar (value & mask ? '1' : '0');
value <<= 1;
}
printf ("\nHope this helps!");
}

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

I have tried the following program:

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

void reverse (int );

int main()
{
int value;

printf ("\nEnter the Integer to be reversed: ");
scanf  ("%d", &value);

reverse (value);
}

void reverse (int value)
{
int mask = 1 << 31;
int i;

for (i=0; i<=31; i++)
{
putchar (value & mask ? '1' : '0');
value <<= 1;
}
printf ("\nHope this helps!");
}``````

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.