a:除法
b:移位
c:减法(一下子不好想到)
d:穷举
e:穷举(直接查表,速度快)
- #include <stdio.h>
-
-
int Count1(char v);
-
int Count2(char v);
-
int Count3(char v);
-
int Count4(char v);
-
int Count5(char v);
-
-
int
-
main(void)
-
{
-
char v = 0x04;
-
-
printf("count1 = %d\n", Count1(v));
-
printf("count2 = %d\n", Count2(v));
-
printf("count3 = %d\n", Count3(v));
-
printf("count4 = %d\n", Count4(v));
-
printf("count5 = %d\n", Count5(v));
-
-
return (0);
-
}
-
-
/*
-
* a
-
*/
-
int
-
Count1(char v)
-
{
-
int num = 0;
-
while (v) {
-
if (v % 2 == 1) {
-
++num;
-
}
-
v /= 2;
-
}
-
return (num);
-
}
-
-
/*
-
* b
-
*/
-
int
-
Count2(char v)
-
{
-
int num = 0;
-
while (v) {
-
num += v & 0x01;
-
v >>= 1;
-
}
-
return (num);
-
}
-
-
/*
-
* c
-
*/
-
int
-
Count3(char v)
-
{
-
int num = 0;
-
while (v) {
-
v &= (v-1);
-
++num;
-
}
-
return (num);
-
}
-
-
/*
-
* d
-
*/
-
int
-
Count4(char v)
-
{
-
int num = 0;
-
switch (v) {
-
case 0x00: num = 0; break;
-
//...
-
}
-
return (num);
-
}
-
-
/*
-
* e
-
*/
-
int
-
Count5(char v)
-
{
-
int countTable[256] =
-
{
-
//...
-
};
-
return (0);
-
}
阅读(541) | 评论(0) | 转发(0) |