Cisco Systems Interview Question: What does the following funct... | Glassdoor

Interview Question

Software Engineer Interview Austin, TX

What does the following function do: unsigned int doit(int

  x) { unsigned int z = 0; while (x &= (x-1)) z++; return z; }
Answer

Interview Answer

3 Answers

1

This is the question with the unintentional bug in it. I ran through it 4 times and kept getting non-sensical answers. It was supposed to count the number of enabled bits in x, but it is off by one.

Interview Candidate on May 28, 2010
0

count no of ones each time it chops 1

r on Jan 27, 2013
0

doti() returns 0 if x is a power of 2:

x -- doit(x)
1 -> 0
2 -> 0
3 -> 1
4 -> 0
8 -> 0

A on Apr 17, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.