您的位置:首页 > 编程语言 > C语言/C++

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

2015-12-08 16:38 405 查看
## 位测试相关代码

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: