3.编写一个函数接受一个int类型的参数并返回该参数中打开位的数量。在一个程序中测试该函数。#include stdio.h int function1(int); int main(void) { int num; printf(enter a integer:); if(scanf( %d,num)1) { printf(%d has %d bits opened.\n,num,function1(num)); } return 0; } int function1(int n) { int result 0; while (n) // 当 n 不为 0 时继续 { n n - 1; // 清除最低位的 1 result; } return result; }
C Primer Plus第六版第15章编程练习第3题
3.编写一个函数接受一个int类型的参数并返回该参数中打开位的数量。在一个程序中测试该函数。#include stdio.h int function1(int); int main(void) { int num; printf(enter a integer:); if(scanf( %d,num)1) { printf(%d has %d bits opened.\n,num,function1(num)); } return 0; } int function1(int n) { int result 0; while (n) // 当 n 不为 0 时继续 { n n - 1; // 清除最低位的 1 result; } return result; }