一个经典的位运算(忘记从哪搬过来的了,貌似不是自己写的)
2013-06-25 12:19
288 查看
/* dualview.c -- bit fields and bitwise operators */ #include <stdio.h> /* BIT-FIELD CONSTANTS */ /* opaque and show */ #define YES 1 #define NO 0 /* line styles */ #define SOLID 0 #define DOTTED 1 #define DASHED 2 /* primary colors */ #define BLUE 4 #define GREEN 2 #define RED 1 /* mixed colors */ #define BLACK 0 #define YELLOW (RED | GREEN) #define MAGENTA (RED | BLUE) #define CYAN (GREEN | BLUE) #define WHITE (RED | GREEN | BLUE) /* BITWISE CONSTANTS */ #define OPAQUE 0x1 #define FILL_BLUE 0x8 #define FILL_GREEN 0x4 #define FILL_RED 0x2 #define FILL_MASK 0xE #define BORDER 0x100 #define BORDER_BLUE 0x800 #define BORDER_GREEN 0x400 #define BORDER_RED 0x200 #define BORDER_MASK 0xE00 #define B_SOLID 0 #define B_DOTTED 0x1000 #define B_DASHED 0x2000 #define STYLE_MASK 0x3000 const char * colors[8] = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"}; struct box_props { unsigned int opaque : 1; unsigned int fill_color : 3; unsigned int : 4; unsigned int show_border : 1; unsigned int border_color : 3; unsigned int border_style : 2; unsigned int : 2; }; union Views /* look at data as struct or as unsigned short */ { struct box_props st_view; unsigned int ui_view; }; void show_settings(const struct box_props * pb); void show_settings1(unsigned short); char * itobs(unsigned int n, char * ps); int main(void) { /* create Views object, initialize struct box view */ union Views box = {{YES, YELLOW , YES, GREEN, DASHED}}; char bin_str[8 * sizeof(unsigned int) + 1]; printf("Original box settings:\n"); show_settings(&box.st_view); printf("\nBox settings using unsigned int view:\n"); show_settings1(box.ui_view); printf("bits are %s\n", itobs(box.ui_view,bin_str)); box.ui_view &= ~FILL_MASK; /* clear fill bits */ box.ui_view |= (FILL_BLUE | FILL_GREEN); /* reset fill */ box.ui_view ^= OPAQUE; /* toggle opacity */ box.ui_view |= BORDER_RED; /* wrong approach */ box.ui_view &= ~STYLE_MASK; /* clear style bits */ box.ui_view |= B_DOTTED; /* set style to dotted*/ printf("\nModified box settings:\n"); show_settings(&box.st_view); printf("\nBox settings using unsigned int view:\n"); show_settings1(box.ui_view); printf("bits are %s\n", itobs(box.ui_view,bin_str)); return 0; } void show_settings(const struct box_props * pb) { printf("Box is %s.\n", pb->opaque == YES? "opaque": "transparent"); printf("The fill color is %s.\n", colors[pb->fill_color]); printf("Border %s.\n", pb->show_border == YES? "shown" : "not shown"); printf("The border color is %s.\n", colors[pb->border_color]); printf ("The border style is "); switch(pb->border_style) { case SOLID : printf("solid.\n"); break; case DOTTED : printf("dotted.\n"); break; case DASHED : printf("dashed.\n"); break; default : printf("unknown type.\n"); } } void show_settings1(unsigned short us) { printf("box is %s.\n", us & OPAQUE == OPAQUE? "opaque": "transparent"); printf("The fill color is %s.\n", colors[(us >> 1) & 07]); printf("Border %s.\n", us & BORDER == BORDER? "shown" : "not shown"); printf ("The border style is "); switch(us & STYLE_MASK) { case B_SOLID : printf("solid.\n"); break; case B_DOTTED : printf("dotted.\n"); break; case B_DASHED : printf("dashed.\n"); break; default : printf("unknown type.\n"); } printf("The border color is %s.\n", colors[(us >> 9) & 07]); } /* convert int to binary string */ char * itobs(unsigned int n, char * ps) { int i; static int size = 8 * sizeof(unsigned int); for (i = size - 1; i >= 0; i--, n >>= 1) ps[i] = (01 & n) + '0'; ps[size] = '\0'; return ps; }
相关文章推荐
- 当网卡收到一个包的目的地址不是自己的地址时
- 很经典的问题,给一个4399的网址,自己玩一下就知道了。。。。。
- 一个关于的指针的经典笔试题目,加上自己的一点体会
- 发布一个自己写的asp框架,注意是asp,不是asp.net
- 刚刚从微博转到博客,筛选一下不是随手记录的东西转过来,这是一个tableviewcontroller的发现
- 管理一个项目团队的问题就是要实现以十当一,而不是以一当十!世界上没有任何两个人是完全相同的,任何人管理自己的方式也没有完全一样的。因而以一当十的英雄式的工作方式不难实现,难的是把这些能以一当十的英雄聚到一起,变成以十当一的团队工作方式
- 发布一个自己写的asp框架,注意是asp,不是asp.net
- 小明同学喜欢体育锻炼,他常常去操场上跑步。跑道是一个圆形,在本题中,我们认为跑道是一个半径为R的圆形,设圆心的坐标原点(0,0)。小明跑步的起点坐标为(R,0),他沿着圆形跑道跑步,而且一直沿着一个方向跑步。回到家后,他查看了自己的计步器,计步器显示他跑步的总路程为L。小明想知道自己结束跑步时的坐标,但是他忘记自己是沿着顺时针方向还是逆时针方向跑的了。他想知道在这两种情况下的答案分别是多少。
- 如何给自己各种帐号编一个安全又不会忘记的密码?
- 通过了软件设计师的考试,虽然不是很有含金量,但也算是对自己的一个总结,对自己能力的一个肯定.
- 单链表的建立程序 没想到自己用的是最笨的尾插法 不要忘记了p从第一个节点开始而不是从头结点开始计数,因为头结点数据值为空
- 铁饭碗的真正含义不是在一个地方吃一辈子饭,而是一辈子到哪儿都有饭吃 ------经典语录
- 当一个用户不是用自己的windowss账户,而是用windows group登陆时,如何查询他的权限?
- 一个简单地list侧滑菜单,自己实现不是梦
- spring经典笔记(部分内容来自一个技术公众号整合了自己的理解)
- 近来玩星际争霸,发觉一个原来同事的水平提高好快,而自己。。貌似止步不前 apm仍然在160左右
- 一个工作四个月的菜鸟的自叙,一哥们写的,看后很感人,转过来激励自己
- 忘记一个不爱自己的人
- 关于solr指导文档,自己做的翻译(有的地方翻译的不是很准确,但是我觉的可以作为一个指导)
- 类成员函数指针的特殊之处(成员函数指针不是指针,内含一个结构体,需要存储更多的信息才能知道自己是否virtual函数)