c语言的bit类型
所以我想问下c语音那有没有bit类型,可以直接对它进行0和1的赋值。
//以下可以不看
我的原本想法是通过编码得到int型的0和1,然后手动进行二进制转十进制操作,得到一个int型的
十进制数,并强制转化成8位的char型进行储存。但有一个bug,最后的时候编码可能不满8位,算法就会出错。。
求高手帮忙 展开
C语言中bit类型是“位”数据类型,一般在嵌入式软件中应用较多,用于IO口的控制,可以是单独申明位变量,也可以申请位结构数据类型,比如:
struct st_flash { /* struct FLASH */
union { /* FLMCR1 */
unsigned char BYTE; /* Byte Access */
struct { /* Bit Access */
unsigned char FWE :1; /* FWE */
unsigned char SWE1:1; /* SWE1 */
unsigned char ESU1:1; /* ESU1 */
unsigned char PSU1:1; /* PSU1 */
unsigned char EV1 :1; /* EV1 */
unsigned char PV1 :1; /* PV1 */
unsigned char E1 :1; /* E1 */
unsigned char P1 :1; /* P1 */
} BIT;
} FLMCR1;
union { /* FLMCR2 */
unsigned char BYTE; /* Byte Access */
struct { /* Bit Access */
unsigned char FLER:1; /* FLER */
unsigned char SWE2:1; /* SWE2 */
unsigned char ESU2:1; /* ESU2 */
unsigned char PSU2:1; /* PSU2 */
unsigned char EV2 :1; /* EV2 */
unsigned char PV2 :1; /* PV2 */
unsigned char E2 :1; /* E2 */
unsigned char P2 :1; /* P2 */
} BIT;
} FLMCR2;
union { /* EBR1 */
unsigned char BYTE; /* Byte Access */
struct { /* Bit Access */
unsigned char EB7:1; /* EB7 */
unsigned char EB6:1; /* EB6 */
unsigned char EB5:1; /* EB5 */
unsigned char EB4:1; /* EB4 */
unsigned char EB3:1; /* EB3 */
unsigned char EB2:1; /* EB2 */
unsigned char EB1:1; /* EB1 */
unsigned char EB0:1; /* EB0 */
} BIT;
} EBR1;
union { /* EBR2 */
unsigned char BYTE; /* Byte Access */
struct { /* Bit Access */
unsigned char EB15:1; /* EB15 */
unsigned char EB14:1; /* EB14 */
unsigned char EB13:1; /* EB13 */
unsigned char EB12:1; /* EB12 */
unsigned char EB11:1; /* EB11 */
unsigned char EB10:1; /* EB10 */
unsigned char EB9 :1; /* EB9 */
unsigned char EB8 :1; /* EB8 */
} BIT;
} EBR2;
};
那好,我可以理解为所有文件都是最低以8位为一个单位来储存的吗
是的。