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.