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

- Girish September 03, 2008 | Flag Reply
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

- Anonymous November 24, 2008 | Flag
Comment hidden because of low score. Click to expand.
1
of 0 vote

can anybody explain in detail whats exactly going on here ?

- abc October 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- jay99 November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is s in the last line?

- buckCherry May 13, 2009 | Flag
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);

- carmack February 13, 2009 | Flag Reply
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;

}

- anon March 16, 2009 | Flag Reply
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;
}

- Tom July 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it should be

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

- tbag March 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Use this:-
Reverse_Number = Number xor 0xFFFFFFFF;

- hi July 28, 2010 | Flag Reply
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..

- Anonymous February 15, 2011 | Flag Reply
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;
}

- Ashish19121 February 26, 2013 | Flag Reply
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);
	}
}

- naga.at.work June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned char revbits[16] = {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);
}

- Anonymous August 27, 2014 | Flag Reply
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);
}

- Toseef February 16, 2015 | Flag Reply
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!");
}

- Anonymous February 14, 2011 | Flag Reply
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!");
}

- Anonymous February 14, 2011 | Flag Reply
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!");
}

- Anonymous February 14, 2011 | Flag Reply


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