## NVIDIA Interview Question

Software Engineer / Developersooh 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

how about this simle approach..

// 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

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

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

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

}

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!");

}

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!");

}

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!");
}
```

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

- Girish September 03, 2008#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 );

}