## NVIDIA Interview Question for Software Engineer / Developers

Country: United States

Comment hidden because of low score. Click to expand.
11
of 15 vote

Good enough

``````int numbits(unsigned int n)
{
for( int i=0; n > 0 ; i++ ) n &= (n-1);
return i;
}``````

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

This code won't work. Your variable i does not exist outside of the for loop.
You just need to instantiate it outside of the for loop and then, issue solved.

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

In Java:

``````public static int getNOnes(int n)
{
int result = 0;
while(n>0)
{
n = n&(n-1);
result++;
}
return result;
}``````

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

#!/usr/bin/env python
def countOne(n): return n != 0 and 1 + countOne(n & (n-1)) or 0

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

This solution is quickrrrrrrr. even for all 16 bits it is O(8)

``````#include<stdio.h>
void main() {
unsigned int num= 65535,no_of_bits=0;
for( ;num>0 ; num = num>>2  )
no_of_bits+=(num & 3)> 1 ? ((num & 3) - 1) : (num & 3 );
printf("%d",no_of_bits);
}``````

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

/* Compile and Executed this code in Linux GCC 3.2*/

#include <stdio.h>

int count(int n)
{
int i=0;
if(n==0)
return 0;
else
{
while (n>0)
{
if(n&1)
++i;
n>>=1;
}
return i;
}
}

int main()
{
int n;
printf ("Enter no\n");
scanf ("%d",&n);
printf ("no of bit that are 1 one in this number is %d\n",count(n));
return 0;
}

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

public class shift {

public static void main(String str[]){
int a=20;
int b=1;
int count=0;;
for(int i=0;i<4*8;i++){
int c = b&a;
if(c>0){
count++;
}

b=b<<1;
}

System.out.println(count);
}
}

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

``````// Return bits that are 1 in the m 1-bit time
short return_bit_count(int num)
{
short count = 0;
while (num > 0)
{
++count;
num &= (num-1);
}
return count;``````

}

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

int numOfOneBits(int number)
{
int count = 0, leftShift = 0;

while(number >= (1 << leftShift))
{
if(number & (1 << leftShift))
count++;

leftShift++;
}

return count;
}

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

void countOne(int n){
int cnt=0;
while(n>0){
if(n&1) cnt++;
n = n>>1;

}

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

void countOne(int n){
int cnt=0;
while(n>0){
n = n & (n-1);
cnt++;
}
printf("num is = %d", cnt);
}

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

``````int n=15;
int count=0;
int i=0;
int log = 0;
//compute the log2 of n(number of bynary chars)
while (n >>= 1) ++log;
n=15;
for( i=0;i<=log;i++){
if(n%2==1){
count++;
}
n=n/2;
}

printf("%i",count);``````

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

this is for only 32 bit integer

#include<stdio.h>
int main()
{
int a=65535,i =0,tmp = 0;
while(i<32)
{
if(((a>>i)&1) == 1)
tmp++;
i++;
}
printf("total no of 1 is %d\n",tmp);

return 0;
}

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

for 32 bit ineteger
#include<stdio.h>
int main()
{
int a=65535,i =0,tmp = 0;
while(i<32)
{
if(((a>>i)&1) == 1)
tmp++;
i++;
}
printf("total no of 1 is %d\n",tmp);

return 0;
}

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

for 32 bit

``````#include<stdio.h>
int main()
{
int a=65535,i =0,tmp = 0;
while(i<32)
{
if(((a>>i)&1) == 1)
tmp++;
i++;
}
printf("total no of 1 is %d\n",tmp);

return 0;
}``````

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

``````int main(){
unsigned int n = 34; // say this is the given number
int count = 0;
while(n){
n &= (n-1);
count++;
}
cout << count << endl;
return 0;
}``````

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

One of the very basic implementations of the above algorithm is to use the & operator to & it with the number 1 to check if the current bit is 1 or not if yes then increment the count variable else skip the bit and then right shift the bit.

Implementation:

``````#include<bits/stdc++.h>
using namespace std;
void countbits(int x){
int count = 0;
while(x){
if(x & 1 == 1)
count++;
x = x >> 1;
}
return count;``````

}

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.