The first entry & #039; C & #039; is an example.

Source: Internet
Author: User

When we first enter 'C', we will be able to give an example.
Question: Write a function to return the number of 1 in the binary parameter. Method 1: I wrote it myself and used '%' and '/' To make it quite simple. Int count_one_bit (int num) {unsigned int count = 0; while (num) {if (num % 2 = 1) count ++; num = num/2 ;} return count;} int main () {int n = 0; int count = 0; scanf ("% d", & n); count = count_one_bit (n ); printf ("% d \ n", count); system ("pause"); return 0;} Method 2: Shift '> ', I am not familiar with the use of shift operators, so I have to exercise more in the future. Int count_one_bit (int num) {int count = 0; int I = 32; while (I --) {if (num & 1 = 1) count ++; num = num> 1;} return count;} method 3: Using '&' and shift, this is really hard to think of. I can think of it only when I think of mathematics! It's strange that I am not good at mathematics. I have to strengthen algorithm learning, engage more, and accumulate experience. Int count_one_bit (int num) {int count = 0; while (num) {count ++; num = num & (num-1) ;}return count ;}summary: when I first entered 'C', I felt that it was logical to write the code! Great! Baidu just gave a look. The above shows a variety of methods, but it is really too weak. Some even don't even understand, so sad! Come on! I have already chosen to go the programmer's path, so don't complain, don't be lazy, don't find excuses and reasons to indulge yourself, stay away from movies, stay close to bed, study hard for one year! I always believe that, if you work hard, you may not succeed, but if you do not work hard, you will not succeed! No matter what the final result is, I will not hate myself, I will not regret it, and my heart will be beautiful.

Related Article

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.