Domanda di colloquio di Microsoft

Count bits in a byte.

Risposte di colloquio

Anonimo

21 nov 2009

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

1

Anonimo

12 ago 2011

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

Anonimo

1 ott 2011

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

Anonimo

6 mag 2009

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

Anonimo

30 mag 2009

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

Anonimo

9 lug 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; }

Anonimo

17 ott 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?

Anonimo

17 ott 2009

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

Anonimo

30 mag 2009

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

Anonimo

5 mag 2009

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