Domanda di colloquio di Qualcomm

Write a C function to return the number of set bits in an integer.

Risposte di colloquio

Anonimo

10 nov 2014

This looks mos efficient, int GetNumSetBits( int anInt ) { int count ; while(anInt) { count += (anInt&0x1); anInt >>= 1; } return count; }

1

Anonimo

12 lug 2017

while(n) { n = n&(n-1) count++; } return count;

1

Anonimo

30 ott 2019

#define BITS_IN_BYTE int numSetBits (int

Anonimo

30 ott 2019

// C language implementation, handles different integer sizes #define BITS_IN_BYTE 8L #define LS_BIT_MASK 1L int numSetBits (int intNumber) { int numBits = 0; for (int i = 0; i >= 1; // right shift LS bit out } }

Anonimo

9 ago 2012

int GetNumSetBits( int anInt ) { int count = 0; for( sizeof( int) ) { count += (anInt & 0x01); anInt >>= 1; } return count; }

Anonimo

22 set 2011

static const int NumberOfSetBits[8] = { /* in nibble, half byte */ 0, /* 0x0, b0000 */ 1, /* 0x1, b0001 */ 1, /* 0x2, b0010 */ 2, /* 0x3, b0011 */ 1, /* 0x4, b0100 */ 2, /* 0x5, b0101 */ 2, /* 0x6, b0110 */ 3 /* 0x7, b0111 */ 1 /* 0x8, b1000 */ 2 /* 0x9, b1001 */ 2 /* 0xA, b1010 */ 3 /* 0xB, b1011 */ 2 /* 0xC, b1100 */ 3 /* 0xD, b1101 */ 3 /* 0xE, b1110 */ 4 /* 0xF, b1111 */ }; #define NumBytesInInt 4 /* assumes 32bit system */ int GetNumberOfSetBits(int anInteger) { uint8_t buf[NumBytesInInt ]; int i; int count = 0; memcpy(buf, &anInteger, sizeof(buf)); for (i=0; i> 4]; } return count; }

Anonimo

19 gen 2011

32 bits in an integer(int) data type on most systems.

2