第 15 章 位操作(dualview)
/*-----------------------------------
dualview.c -- 位字段和按位运算符
-----------------------------------*/ #include <stdio.h>
#include <limits.h> //边框线样式
#define SOLID 0
#define DOTTED 1
#define DASHED 2 //三原色
#define BLUE 4
#define GREEN 2
#define RED 1 //混合颜色
#define BLACK (RED & GREEN & BLUE)
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (GREEN | BLUE)
#define WHITE (RED | BLUE | GREEN) //按位方法中用到的符号常量
#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 struct box_props
{
unsigned int opaque : ;
unsigned int fill_color : ;
unsigned int : ;
unsigned int show_border : ;
unsigned int border_color : ;
unsigned int border_style : ;
unsigned int : ;
}; //把数据看作结构或 unsigned short 类型的变量
union Views
{
struct box_props st_view;
unsigned short us_view;
}; const char *color[] =
{
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
}; void show_settings(const struct box_props *pb);
void show_setting1(unsigned short);
char* itobs(int n, char *ps);
void show_bstr(const char *str); int main()
{
printf("size of struct box_props is %d bit\n", CHAR_BIT * sizeof(struct box_props));
printf("size of unsigned short is %d bit\n", CHAR_BIT * sizeof(unsigned short)); //创建 Vies 联合,并初始化 initialize struct box view
union Views box = {{true, YELLOW, true, GREEN, DASHED}};
char bin_str[ * sizeof(unsigned int) + ]; printf("\nOriginal box settings:\n");
show_settings(&box.st_view);
printf("\nBox settings using unsigned int view:\n");
show_setting1(box.us_view);
//printf("bits are %s\n", itobs(box.us_view, bin_str));
printf("bits are:\n");
show_bstr(itobs(box.us_view, bin_str)); box.us_view &= ~FILL_MASK; //把表示填充色的位清零
box.us_view |= CYAN; //重置填充色
box.us_view ^= OPAQUE; //切换透明位
box.us_view &= ~BORDER_MASK; //边框颜色位清零
box.us_view |= BORDER_RED;
box.us_view &= ~STYLE_MASK; //样式位清零
box.us_view |= B_DOTTED; //边框样式设置为点
printf("\nModified box settings:\n");
show_settings(&box.st_view);
printf("\nBox settings using unsigned int view:\n");
show_setting1(box.us_view);
//printf("bits are %s\n", itobs(box.us_view, bin_str));
printf("bits are:\n");
show_bstr(itobs(box.us_view, bin_str)); return ;
} void show_settings(const struct box_props *pb)
{
printf("Box is %s.\n", pb->opaque ? "opaque" : "transparent");
printf("The fill color is %s.\n", color[pb->fill_color]);
printf("Border %s.\n", pb->show_border ? "shown" : "not shown");
printf("The border color is %s.\n", color[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");
break;
}
} void show_setting1(unsigned short us)
{
printf("Box is %s.\n", (us & OPAQUE) ? "opaque" : "transparent");
printf("The fill color is %s.\n", color[(us >> ) & 0x07]);
printf("Border %s.\n", (us & BORDER) == BORDER ? "shown" : "not shown");
printf("The border color is %s.\n", color[us >> & 0x07]); 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 style");
break;
}
} char* itobs(int n, char *ps)
{
const static int size = CHAR_BIT * sizeof(int); for (int i(size - ); i >= ; --i, n >>= )
ps[i] = ( & n) + ''; ps[size] = '\0'; return ps;
} //以 4 位为一组,显示二进制字符串
void show_bstr(const char *str)
{
int i = ; while (str[i]) //str[i] 不是空字符
{
putchar(str[i]); if (++i % == && str[i])
putchar(' ');
} putchar('\n');
}
dualview.c

第 15 章 位操作(dualview)的更多相关文章
- 第 15 章 位操作(invert4)
/*------------------------------------ invert4.c -- 使用位操作显示二进制 ------------------------------------* ...
- 第 15 章 位操作(binbit)
/*------------------------------------ binbit.c -- 使用位操作显示二进制 ------------------------------------*/ ...
- 第 15 章 位操作(fields)
/*----------------------------------- fields.c -- 定义并使用字段 -----------------------------------*/ #inc ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- 【STM32H7教程】第15章 STM32H7的GPIO基础知识(重要)
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第15章 STM32H7的GPIO基础知识(重要) ...
- 第15章 LinkedList类(暂无)
第15章 LinkedList类 LinkedList类是
- ASM:《X86汇编语言-从实模式到保护模式》第15章:任务切换
15章其实应该是和14章相辅相成的(感觉应该是作者觉得14章内容太多了然后切出来了一点).任务切换和14章的某些概念是分不开的. ★PART1:任务门与任务切换的方法 1. 任务管理程序 14章的时候 ...
- 第15章 设备无关位图_15.3 DIB和DDB的结合
第15章 设备相关位图_15.3 DIB和DDB的结合 15.3.1 从DIB创建DDB (1)hBitmap =CreateDIBitmap(…)——注意这名称会误导,实际上创建的是DDB 参数 说 ...
- unix network programming(3rd)Vol.1 [第13~15章]《读书笔记系列》
第13章 守护进程和inetd 超级服务器 syslog() daemon_init() setuid() setgid() 第14章 高级IO 标准I/O函数库,支持3种缓冲 缓冲(读写存储设备(硬 ...
随机推荐
- SQOOP安装部署
1.环境准备 1.1软件版本 sqoop-1.4.5 下载地址 2.配置 sqoop的配置比较简单,下面给出需要配置的文件 2.1环境变量 sudo vi /etc/profile SQOOP_HOM ...
- ETCD&Flannel安装
.ETCD 安装: nohup etcd --name etcd0 \ --advertise-client-urls http://172.31.24.246:2379,http://127.0.0 ...
- SpringBoot2.0源码分析(二):整合ActiveMQ分析
SpringBoot具体整合ActiveMQ可参考:SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQ ActiveMQ自动注入 当项目中存在javax.jms.Me ...
- 进程间通信IPC-命名管道FIFO
FIFO又被称为命名管道,未命名的管道只能在两个相关的进程之间使用,而这两个相关的进程还要有一个共同创建了它们的祖先进程,但是FIFO,不相关的进程之间也能交换数据. FIFO是一种文件类型.通过st ...
- (转)创建GitHub技术博客
https://blog.csdn.net/renfufei/article/details/37725057
- 经济学人使用Golang构建微服务历程回顾
关键点 经济学人内容分发系统需要更大的灵活性,将内容传递给日益多样化的数字渠道.为了实现这一灵活性目标并保持高水平的性能和可靠性,平台从一个单体结构过渡到微服务体系结构. 用Go编写的服务是新系统的一 ...
- swagger 常用注解说明
本内容引用自:https://blog.csdn.net/u014231523/article/details/76522486 常用注解: - @Api()用于类: 表示标识这个类是swagger的 ...
- 团队作业4——beta阶段冲刺
Deadline: 2018-12-16 22:00PM,以博客提交至班级博客时间为准 提交: (a) 项目课堂演示: (b) 1篇冲刺准备+5篇冲刺随笔,: (c) 1篇用户使用用报告: (d) 1 ...
- Maven教程1(介绍安装和配置)
官网地址:http://maven.apache.org/ 1.Maven介绍 1.1为什么需要使用Maven 之前学Spring和SpringMVC的时候我们需要单独自己去找相关的jar. 这些ja ...
- KM算法及其应用
在二分图匹配中有最大匹配问题,使用匈牙利算法或者网络流相关算法解决,如果给每条边增加一个权值,求权值和最大的匹配方案就叫做最大权匹配问题.其实之前所说的最大匹配就是权值为1的最大权匹配. 求最大权完备 ...