1.写一个函数返回参数二进制中1的个数
代码:
方法1:
#include <stdio.h>
int count_num_of_1(int n)
{
int count = 0;
while (n)
{
if ((n % 2) == 1)
{
count++;
}
n /= 2;
}
}
int main()
{
int num = 0;
scanf("%d", &num);
int n = count_num_of_1(num);
printf("%d\n", n);
return 0;
}
2.判断大端小端
代码:
//判断大端小端
#include <stdio.h>
int main()
{
int a = 1;
//00000000 000000000 00000000 00000001
// 0x 00 00 00 01
//小端:01 00 00 00
低 高
//大端:00 00 00 01
if (*(char*)&a == 1)
{
printf("小端\n");
}
else
printf("大端\n");
return 0;
}