发布网友 发布时间:2022-12-02 13:54
共1个回答
热心网友 时间:2023-11-10 09:31
{追答我之前看错了,我还以为不要前面的0呢
还有
a[0] = 0x08 = 00 00000 00000 00000 00000 01000
整形是32位的...不是8位...
#include <stdio.h>
#include <math.h>
int cal(int arr[], int len)
{
int pos = 0, sum = 0;
while(pos < len)
{
if(arr[pos])
sum += pow(2, pos);
pos++;
}
pos = 0;
while(pos < len)
{
arr[pos] = 0;
pos++;
}
return sum;
}
int main(void)
{
int a[] = {0x08,0x15,0x41,0x0C,0x27};
int arr[5] = {};
int b = 0, num = 0;
int len - sizeof(a)/sizeof(int);
int loop = 0,int pos =0, pos1 = 0;
while(loop < len)
{
pos = 0;
b = 0;
while(pos<5)
{
arr[pos]=a[loop]&1;
pos++;
a[loop]>>=1;
if(pos1 >= 31)
{
pos1 = 0;
loop++;
if(loop>=len)
break;
}
else
{
pos1++;
}
}
b = cal(arr, sizeof(arr)/sizeof(int));
printf("sum:%d\n", b);
num++;
}
printf("num:%d\n", num);
getchar();
return 0;
}