您正在一對夫婦不幸的假設:
- 的
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