在这里,我得到了一个以二进制表示形式打印整数的代码
#define BUF_SIZE 33
int main() {
for (int count = 0; count <=25; count++){
char buffer[BUF_SIZE];
buffer[BUF_SIZE - 1] = '\0';
int2bin(count, buffer, BUF_SIZE - 1);
printf("%d = %s \n",count, buffer);
}
}
char *int2bin(int a, char *buffer, int buf_size) {
buffer += (buf_size - 1);
for (int i = 31; i >= 0; i--) {
buffer--;
*buffer = (a & 1) + '0';
a >>= 1;
}
return buffer;
}
它工作得很好,直到我试图挤压一些代码行替换
int2bin(count, buffer, BUF_SIZE - 1);
printf("%d = %s \n",count, buffer);
和
printf("%d = %s \n",count, int2bin(count, buffer, BUF_SIZE - 1));
这些日志完全破坏了我的输出:
805306368 = 00000000000000000000000000000000
805306369 = 00000000000000000000000000000001
805306370 = 00000000000000000000000000000010
805306371 = 00000000000000000000000000000011
805306372 = 00000000000000000000000000000100
805306373 = 00000000000000000000000000000101
等等...
你能解释一下为什么吗?