您正在一对夫妇不幸的假设:
- 的
int
并不总是32位
- 你有4个
int
变量,而不是128倍“一位”的数组变量
喜欢的东西是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h> /* uint32_t comes from here */
void main(void) {
int i, j;
uint32_t t;
uint32_t data[4];
/* populate the data */
for (i = 0; i < 4; i++) {
data[i] = rand();
}
/* print out the 'bits' for each of the four 32-bit values */
for (i = 0; i < 4; i++) {
t = data[i];
/* print out the 'bits' for _this_ 32-bit value */
for (j = 0; j < (sizeof(data[0]) * 8); j++) {
if (t & 0x80000000) {
printf("1");
} else {
printf("0");
}
t <<= 1;
}
printf("\n");
}
}
输出:
01101011100010110100010101100111
00110010011110110010001111000110
01100100001111001001100001101001
01100110001100110100100001110011
别t垃圾邮件标签。这不是C.并且不要发布文字的图像。提供[mcve]。 – Olaf
如果您声明了4个int元素的数组,则引用索引0 ... 3之外的任何元素将调用* undefined behavior *,这是您的代码在该循环过程中执行124次的一些操作。 – WhozCraig
@WhozCraig谢谢,那么你能指导我解决它的一些方法吗? – Van