“占10位的内存”是什么意思?
以字节为单位来说,也要16位。
是不是这意思:
#include <stdio.h>
#include <limits.h>
#define N 10
main()
{
char FileName[]="C:\\TEMP\\test.tmp";
FILE *fp;
int i, a[N]={0,1,1,0,1,0,1,1,0,1};
unsigned short int d=0, dw;
for (i=0; i<N; ++i)
if (a[i]==1)
d += 1<<(N-i-1);
printf("BIN: ");
for (i=0; i<N; ++i)
printf("%d", a[i]);
printf("\nDEC: %d\n", d);
fp = fopen(FileName, "wb");
fwrite(&d, sizeof(d), 1, fp);
fclose(fp);
fp = fopen(FileName, "rb");
fread(&dw, sizeof(dw), 1, fp);
fclose(fp);
printf("BIN: ");
for (i=sizeof(dw)*CHAR_BIT-1; i>=0; --i)
printf("%d", (dw>>i)&1);
printf("\nDEC: %d\n", dw);
}
[此贴子已经被作者于2017-10-26 06:11编辑过]