位段:结构体定义:unsigned int type : 2;

#include "stdio.h"

int main(void)
{
	typedef union
	{
		unsigned char byte;	                /**< the whole byte */
		struct
		{
			unsigned int retain : 1;		/**< retained flag bit */
			unsigned int qos : 2;				/**< QoS value, 0, 1 or 2 */
			unsigned int dup : 1;				/**< DUP flag bit */
			unsigned int type : 4;			/**< message type nibble */
		} bits;
 	} MQTTHeader;
 	MQTTHeader xxx;
 	xxx.byte = 0xD0;
 	printf("MQTTHeader.bits.retain = %d\r\n",xxx.bits.retain);
 	printf("MQTTHeader.bits.qos = %d\r\n",xxx.bits.qos);
 	printf("MQTTHeader.bits.dup = %d\r\n",xxx.bits.dup);
 	printf("MQTTHeader.bits.type = %d\r\n",xxx.bits.type);
}

运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190805141325841.png
结论:
unsigned int retain : 1;表示为unsigned int的第0位,
unsigned int qos : 2; 表示为unsigned int的第1、2位;
unsigned int dup : 1;表示为unsigned int的第3位;
unsigned int type : 4;表示unsigned int的第4、5、6、7位。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
补充完整下面代码:下面是一个完成 BMP 图像反色处理的 c 语言程序的示例: #include <stdio.h> #include <stdlib.h> // 定义 BMP 文件头的结构 typedef struct { unsigned short int type; unsigned int size; unsigned short int reserved1, reserved2; unsigned int offset; } BMPHeader; // 定义 BMP 信息头的结构 typedef struct { unsigned int size; int width, height; unsigned short int planes; unsigned short int bits; unsigned int compression; unsigned int imagesize; int xresolution, yresolution; unsigned int ncolours; unsigned int importantcolours; } BMPInfoHeader; // 定义调色板的结构 typedef struct { unsigned char blue; unsigned char green; unsigned char red; unsigned char reserved; } Palette; int main(int argc, char *argv[]) { // 判断命令行参数是否合法 if (argc != 3) { printf("Usage: %s <input file> <output file>\n", argv[0]); return 1; } // 打开输入文件 FILE *input = fopen(argv[1], "rb"); if (!input) { perror(argv[1]); return 1; } // 打开输出文件 FILE *output = fopen(argv[2], "wb"); if (!output) { perror(argv[2]); return 1; } // 读取文件头 BMPHeader header; fread(&header, sizeof(BMPHeader), 1, input); // 判断文件是否是 BMP 格式 if (header.type != 0x4D42) { fclose(input); fclose(output); fprintf(stderr, "%s is not a BMP file!\n", argv[1]); return 1; } // 读取信息头 BMPInfoHeader info; fread(&info, sizeof(BMPInfoHeader), 1, input); // 判断是否是真彩色或 256 色的图像 if (info.bits != 24 && info.bits != 8) { fclose(input); fclose(output); fprintf(stderr, "Unsupported BMP image!\n"); return 1; } // 写
02-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤梦飞123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值