0
아래의 구조체에 비트 필드의 비트 표현을 인쇄하고 싶습니다. 그러나 나는 내용을 인쇄 할 때 계속해서 첫 번째 비트 필드의 값을 계속해서 보게됩니다. 내가 도대체 뭘 잘못하고있는 겁니까?C의 구조체에 길이 8의 여러 비트 필드를 인쇄하는 방법
#include <stdio.h>
#include <limits.h>
typedef struct Bits {
union BitUnion {
unsigned int op1 : 8;
unsigned int op2 : 8;
unsigned int op3 : 8;
unsigned int op4 : 8;
int num;
} BitUnion;
} Bits;
void printBitNum(Bits b);
int main() {
Bits test;
test.BitUnion.op1 = 2;
test.BitUnion.op2 = 5;
test.BitUnion.op3 = 'd';
test.BitUnion.op4 = 10;
printBitNum(test);
return 0;
}
void printBitNum(Bits b) {
int i;
for (i = (CHAR_BIT * sizeof(int)) - 1; i >= 0; i--) {
if (b.BitUnion.num & (1 << i)) {
printf("1");
} else {
printf("0");
}
}
printf("\n");
}
정확하게 맞습니다! 'struct'와'int'를 모두 호스팅하는 데이터 구조를 변경 한 후에 모든 것이 작동합니다. – Yos