Calculate the number of 1

Source: Internet
Author: User

For a given number, calculate the number of the corresponding binary 1. For example, 9, the number of the corresponding binary is 1001, then the number of corresponding 1 is 2.

Interview review, more than a month, from the Netease Open Class "Programming Paradigm", and then slightly flip the "premier c ++", then read Yan Weimin's data structure, to be honest, not very good. I can't see the last few chapters. I just switched to introduction to algorithms and wrote 20 blogs about data structures and algorithms. Every morning, I insisted on reading the blog on csdn and learned a lot. Next, I plan to take a look at the beauty of programming and practice what I learned. I will read it around March, and I will look at the interview book before the formal recruitment. I have prepared it. As the foundation is very weak, it is still very difficult to continue to fuel.

Back to the truth, this question seems to be an interesting one, both of which are the beauty of programming and the interview book.

For example, data x, as long as the constant x & (x-1) knows that x is 0.

Code:

 

# Include <iostream> using namespace std; int func (int x) {int count = 0; while (x) {count ++; x = x & (x-1 );} return count;} int main () {cout <func (99); return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.