Interview Question

Interview Seattle, WA

Write a function that takes an integer and counts the

  number of bits.
algorithm, bit manipulation

Interview Answer

3 Answers


This is a simple bit manipulation problem. If you study before your interviews, this is a common area to study.

Interview Candidate on Jan 14, 2012

void count_Bits(int inp){ int count = 0; for (int y = inp; y >=1;y = y /2 ){ count++; } cout<< "number of bits : " << count<< endl; }

karan on Nov 17, 2012

If you take a simple log base 2 of the integer, that should give you the number of bits, right?

Anonymous on Jul 21, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.