1.位运算符
- 按位取反~
将所有二进制位按位取反
例如:0000 0000 0000 1101
按位取反:1111 1111 1111 0010
2.按位与&
两个数的二进制位都为1,则相与后为1,否则为0
(1111 0000 1111 0000) & (0101 1010 0101 1101)
结果:0101 0000 0101 0000
3.按位或|
两个数二进制位有一个为1,则为1,否则为0
(0101 1010 0101 1010) | (1010 0101 1010 0101)
结果:1111 1111 1111 1111
4.按位异或^
两个数二进制位不同则为1,相同则为0
(1111 0000 0110 1001) ^(0000 1111 0110 1001)
结果:1111 1111 0000 0000
异或交换两个数
a = a^ b;
b = a^ b;
a = a^ b;
5.按位左移<<
0000 1101 0010
左移一位结果:0001 1010 0100,相当于除2
后面补0
6.按位右移
0011 1100 1110 0000
右移结果:0001 1110 0111 0000
若最高位为1,右移以后左边可能补0也可能补1
最高位为0左边补0
2.掩码
设置位开(1)或关(0)的位组合
flag = 00000011
MASK = 10010110
MASK & flag
flag的前6位二进制为0所以相与以后MASK的前6位为0,被屏蔽调,MASK的后两位还是原来的值
3.位字段
操控位的第二种方法
struct{
unsigned int autfd:1;
unsigned int bldfc:1;
unsigned int undln:1;
unsigned int itals:1;
}prnt;
以上声明了一个4个1位的字段,他们共享一个unsigned int空间,每个都是1位(只能存放0或1)
可以通过.来使用
prnt.itals = 0;
prnt.undln = 1;
位字段可以设置多个长度
struct {
unsigned int code1:2;
unsigned int code2:2;
unsigned int code3:8;
}
3.位字段的使用
黑色 000
红色 001
绿色 010
黄色 011
蓝色 100
紫色 101
青色 110
白色 111
/* fields.c -- 定义并使用字段 */
#include <stdio.h>
#include <stdbool.h>
// C99定义了bool、true、false
/* 线的样式 */
#define SOLID 0
#define DOTTED 1
#define DASHED 2
/* 三原色 */
#define BLUE 4
#define GREEN 2
#define RED 1
/* 混合色 */
#define BLACK 0
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (GREEN | BLUE)
#define WHITE (RED | GREEN | BLUE)
const char * colors[8] = { "black", "red", "green",
"yellow", "blue", "magenta", "cyan", "white" };
struct box_props {
bool opaque : 1;
// 或者 unsigned int (C99以前)
unsigned int fill_color : 3;
unsigned int : 4;
bool show_border : 1;
// 或者 unsigned int (C99以前)
unsigned int border_color : 3;
unsigned int border_style : 2;
unsigned int : 2;
};
void show_settings(const struct box_props * pb);
int main(void)
{
/* 创建并初始化 box_props 结构 */
struct box_props box = { true, YELLOW, true, GREEN,
DASHED };
printf("Original box settings:\n");
show_settings(&box);
box.opaque = false;
box.fill_color = WHITE;
box.border_color = MAGENTA;
box.border_style = SOLID;
printf("\nModified box settings:\n");
show_settings(&box);
return 0;
}
void show_settings(const struct box_props * pb) {
printf("Box is %s.\n",pb->opaque == true ? "opaque" : "transparent");
printf("The fill color is %s.\n", colors[pb->fill_color]);
printf("Border %s.\n",pb->show_border == true ? "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");
}
}
4.位字段和位运算符的混合使用
/* dualview.c -- 位字段和按位运算符 */
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
/* 位字段符号常量 */
/* 边框线样式
*/
#define SOLID 0
#define DOTTED 1
#define DASHED 2
/* 三原色 */
#define BLUE 4
#define GREEN 2
#define RED 1
/* 混合颜色 */
#define BLACK 0
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (GREEN | BLUE)
#define WHITE (RED | GREEN | BLUE)
/* 按位方法中用到的符号常量 */
#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 {
bool opaque: 1;
unsigned int fill_color: 3;
unsigned int : 4;
bool show_border : 1;
unsigned int border_color : 3;
unsigned int border_style: 2;
unsigned int: 2;
};
union Views
/* 把数据看作结构或unsigned short类型的变量
*/
{
struct box_props st_view;
unsigned short us_view;
};
void show_settings(const struct box_props * pb); void
show_settings1(unsigned short);
char * itobs(int n, char * ps);
int main(void)
{
/* 创建Views联合,并初始化initialize struct box view */
union Views box = { { true, YELLOW, true, 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.us_view);
printf("bits are %s\n",
itobs(box.us_view, bin_str));
box.us_view &= ~FILL_MASK;
box.us_view |= (FILL_BLUE | FILL_GREEN);
box.us_view &= ~STYLE_MASK;
printf("\nModified box settings:\n");
show_settings(&box.st_view);
printf("\nBox settings using unsigned int view:\n");
show_settings1(box.us_view);
printf("bits are %s\n",
itobs(box.us_view, bin_str));
return 0;
}
void show_settings(const struct box_props * pb) {
printf("Box is %s.\n",pb->opaque == true ? "opaque" : "transparent");
printf("The fill color is %s.\n", colors[pb->fill_color]);
printf("Border %s.\n",pb->show_border == true ? "shown" : "not shown");
printf("Thebordercoloris%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" : "notshown");
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]);
}
char * itobs(int n, char * ps)
{
int i;
const static int size = CHAR_BIT * sizeof(int);
for (i = size - 1; i >= 0; i--, n >>= 1) ps[i] = (01 & n) + '0';
ps[size] = '\0';
return ps;
}