#include <cstdio>
void printf_bin2(void *dataPtr, int sizeOfData) {
int i, j;
unsigned char *p = (unsigned char *) dataPtr + (sizeOfData - 1);
for (i = 0; i < sizeOfData; i++)
{
j = *(p - i);
for (int k = 7; k >= 0; k--)
{
if (j & (1 << k))
printf("1");
else
printf("0");
}
printf(" ");
}
printf("\r\n");
}
struct Test1 {
unsigned int x: 5;
unsigned int y: 8;
};
struct Test2 {
unsigned int x: 5;
unsigned int : 0;
unsigned int y: 8;
};
int main() {
printf("Size of test1 is %lu bytes\n",
sizeof(struct Test1));
Test1 test1{0};
test1.x = 1;
test1.y = 2;
printf_bin2(&test1, sizeof(Test1));
printf("Size of test2 is %lu bytes\n",
sizeof(struct Test2));
Test2 test2{0};
test2.x = 1;
test2.y = 2;
printf_bin2(&test2, sizeof(Test2));
return 0;
}
Size of test1 is 4 bytes
00000000 00000000 00000000 01000001
Size of test2 is 8 bytes
00000000 00000000 00000000 00000010 00000000 00000000 00000000 00000001
|