#include <stdio.h>
#include <stdlib.h>
//模拟一个字节的数据 typedef struct _bitStruct{ unsigned char b1:1; unsigned char b2:1; unsigned char b3:1; unsigned char b4:1; unsigned char b5:1; unsigned char b6:1; unsigned char b7:1; unsigned char b8:1; }bitStruct;
int main(void){ //获得4个字节的空间 bitStruct * pBit = (bitStruct *)malloc(sizeof(bitStruct) *4); //将指针强转为int 的型的指针, 模拟int型数据的情况 int * pInt = (int *)pBit; //共享空间 printf("%s\n", "please enter a number for test:"); scanf("%d", pInt); int i; //因为在内存中 数据存储的字节存储是高位字节在前,低位字节在后,所以要倒着打印数据 for(i = 3; i>=0;i--){ //因为在内存中 数据存储是的位也是高位在前,低位在后,所以要倒着打印数据 printf("%d%d%d%d %d%d%d%d ", pBit[i].b8, pBit[i].b7, pBit[i].b6, pBit[i].b5, pBit[i].b4, pBit[i].b3, pBit[i].b2, pBit[i].b1); } printf("\n"); return 0; }