Sollicitatievraag bij Microsoft

Count bits in a byte.

Antwoorden op sollicitatievragen

Anoniem

21 nov 2009

Funny way: while (x != 0) { x = x& (x-1) count++; }

1

Anoniem

12 aug 2011

while (number > 0) { count += (number & 0x01); number >>= 1; }

Anoniem

1 okt 2011

Shifting may not work if the number has a sign bit... will keep pushing in ones. num&num-1 should work

Anoniem

30 mei 2009

better solution: byte input; int count = 0; while (input) { ++count; input >> 1; }

Anoniem

6 mei 2009

byte input; int bits = 0; for(int i = 0; i > i) & 0x01; }

Anoniem

9 jul 2009

The *ONLY* portable way to answer this question such that it is correct for *ALL* ANSI C compilers on *ALL* hardware platforms is as follows: #include #include int main() { printf("Number of bits per byte on this machine with this ANSI C compiler = %d\n", CHAR_BIT); return 0; }

Anoniem

17 okt 2009

i think the question was written wrong by the person i checked on google and the question is Count the "on" bits in a byte?

Anoniem

17 okt 2009

one way to solve this char c; int bits=0; for (int i=0;i>1; } return bits

Anoniem

30 mei 2009

better solution: byte input; int count = 0; while (input) { ++count; cout >> 1; }

Anoniem

5 mei 2009

Its 8 of course, but I WISH I had that question... lol

Sollicitatievragen voor Microsoft: Count bits in a byte. | Glassdoor