C++之位测试练习的相关代码

## 位测试相关代码

#include <iostream>
#include <stdio.h>
#include <arpa/inet.h>

using namespace std;

typedef unsigned int UINT32;

typedef union {
	struct Color{
		UINT32 end:6;
		UINT32 b:8;
		UINT32 g:8;
		UINT32 r:8;
		UINT32 type: 2;
	} color;
	UINT32 data;
} ColorData;

#define TYPE_LEN 2
#define R_LEN 8
#define G_LEN 8
#define B_LEN 8
#define END_LEN 6

UINT32 formColor(UINT32 type,UINT32 r,UINT32 g,UINT32 b,UINT32 end){
	printf("type:\t%u\n",type << 30);
	printf("r:\t%u\n",r << 22);
	printf("g:\t%u\n",g << 14);
	printf("b:\t%u\n",b << 6);
	return (type << 30) + (r << 22) + (g << 14) + (b << 6) + (end);
}

bool isBigEndian(){
	int x = 1;
	char * p = (char *)&x;
	if(*p == 1)
		return false;
	else
		return true;
}


int main(){
	
	ColorData gColorData;
	UINT32 col = formColor(2,60,60,60,20);

	gColorData.data = col;
	printf("htonl:\t%d\n",col);
	printf("Endian-Mode:%d\n",isBigEndian());
	printf("colordata:\t%u\n",col);
	printf("==============================================\n");
	printf("type:\t%d\n",gColorData.color.type);
	printf("r:\t%d\n",gColorData.color.r);
	printf("g:\t%d\n",gColorData.color.g);
	printf("b:\t%d\n",gColorData.color.b);
	printf("end:\t%d\n",gColorData.color.end);
	printf("==============================================\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值