C语言里如何定义任意bit长度的类型来着
答案:2 悬赏:50 手机版
解决时间 2021-03-27 16:43
- 提问者网友:泪痣哥哥
- 2021-03-27 00:26
C语言里如何定义任意bit长度的类型来着
最佳答案
- 五星知识达人网友:woshuo
- 2019-09-04 06:42
下面就是一个例子,定义了一个结构体变量ch,包含多个位域。
struct{
unsigned con: 8;
unsigned forecolor: 4;
unsigned bgcolor: 3;
unsigned blink: 1;
}ch;
struct{
unsigned con: 8;
unsigned forecolor: 4;
unsigned bgcolor: 3;
unsigned blink: 1;
}ch;
全部回答
- 1楼网友:低血压的长颈鹿
- 2020-11-06 19:21
c语言中bit类型是“位”数据类型,一般在嵌入式软件中应用较多,用于io口的控制,可以是单独申明位变量,也可以申请位结构数据类型,比如:
struct st_flash {
union {
unsigned char byte;
struct {
unsigned char fwe :1;
unsigned char swe1:1;
unsigned char esu1:1;
unsigned char psu1:1;
unsigned char ev1 :1;
unsigned char pv1 :1;
unsigned char e1 :1;
unsigned char p1 :1;
} bit;
} flmcr1;
union {
unsigned char byte;
struct {
unsigned char fler:1;
unsigned char swe2:1;
unsigned char esu2:1;
unsigned char psu2:1;
unsigned char ev2 :1;
unsigned char pv2 :1;
unsigned char e2 :1;
unsigned char p2 :1;
} bit;
} flmcr2;
union {
unsigned char byte;
struct {
unsigned char eb7:1;
unsigned char eb6:1;
unsigned char eb5:1;
unsigned char eb4:1;
unsigned char eb3:1;
unsigned char eb2:1;
unsigned char eb1:1;
unsigned char eb0:1;
} bit;
} ebr1;
union {
unsigned char byte;
struct {
unsigned char eb15:1;
unsigned char eb14:1;
unsigned char eb13:1;
unsigned char eb12:1;
unsigned char eb11:1;
unsigned char eb10:1;
unsigned char eb9 :1;
unsigned char eb8 :1;
} bit;
} ebr2;
};
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯