当前位置:网站首页>位的高阶运算
位的高阶运算
2022-07-02 22:06:00 【打酱油的;】
1.概念
可以使用C对变量中的个别位进行操作。您可能对人们想这样做的原因感到奇怪。这种能力有时确实是必须的,或者至少是有用的。C提供位的逻辑运算符和移位运算符。在以下例子中,我们将使用二进制计数法写出值,以便您可以了解对位发生的操作。在一个实际程序中,您可以使用一般的形式的整数变量或常量。例如不适用00011001的形式,而写为25或者031或者0x19.在我们的例子中,我们将使用8位数字,从左到右,每位的编号是7到0。
2.位逻辑运算符
4个位运算符用于整型数据,包括char.将这些位运算符成为位运算的原因是它们对每位进行操作,而不影响左右两侧的位。请不要将这些运算符与常规的逻辑运算符(&& 、||和!)相混淆,常规的位的逻辑运算符对整个值进行操作。
2.1按位取反~
一元运算符~将每个1变为0,将每个0变为1,如下面的例子:
~(10011010) 01100101 |
假设a是一个unsigned char,已赋值为2.在二进制中,2是00000010.于是-a的值为11111101或者253。请注意该运算符不会改变a的值,a仍为2。
unsigned char a = 2; //00000010 unsigned char b = ~a; //11111101 printf("ret = %d\n", a); //ret = 2 printf("ret = %d\n", b); //ret = 253 |
2.2位与(AND): &
二进制运算符&通过对两个操作数逐位进行比较产生一个新值。对于每个位,只有两个操作数的对应位都是1时结果才为1。
(10010011) & (00111101) = (00010001) |
C也有一个组合的位与-赋值运算符:&=。下面两个将产生相同的结果:
val &= 0377 val = val & 0377 |
2.3位或(OR): |
二进制运算符|通过对两个操作数逐位进行比较产生一个新值。对于每个位,如果其中任意操作数中对应的位为1,那么结果位就为1.
(10010011) | (00111101) = (10111111) |
C也有组合位或-赋值运算符: |=
val |= 0377 val = val | 0377 |
2.4位异或:
二进制运算符^对两个操作数逐位进行比较。对于每个位,如果操作数中的对应位有一个是1(但不是都是1),那么结果是1.如果都是0或者都是1,则结果位0.
(10010011) ^ (00111101) = (10101110) |
C也有一个组合的位异或-赋值运算符: ^=
val ^= 0377 val = val ^ 0377 |
2.5用法
1打开位(|)置一
已知:10011010:
- 将位2打开
flag | 10011010
(10011010) |(00000100) =(10011110) |
- 将所有位打开。
flag | ~flag
(10011010) |(01100101) =(11111111) |
2 关闭位(&)置零
flag & ~flag
(10011010) &(01100101) =(00000000) |
3 转置位(^)取反
转置(toggling)一个位表示如果该位打开,则关闭该位;如果该位关闭,则打开。您可以使用位异或运算符来转置。其思想是如果b是一个位(1或0),那么如果b为1则b^1为0,如果b为0,则1^b为1。无论b的值是0还是1,0^b为b.
flag ^ 0xff
(10010011) ^(11111111) =(01101100) |
4 交换两个数不需要临时变量
//a ^ b = temp; //a ^ temp = b; //b ^ temp = a (10010011) ^(00100110) =(10110101) (10110101) ^(00100110) 10010011
int a = 10; int b = 30; |
5判断奇偶(&)


3.移位运算符
现在让我们了解一下C的移位运算符。移位运算符将位向左或向右移动。同样,我们仍将明确地使用二进制形式来说明该机制的工作原理。
3.1 左移 <<
左移运算符<<将其左侧操作数的值的每位向左移动,移动的位数由其右侧操作数指定。空出来的位用0填充,并且丢弃移出左侧操作数末端的位。在下面例子中,每位向左移动两个位置。
(10001010) << 2 (00101000) |
该操作将产生一个新位置,但是不改变其操作数。
1 << 1 = 2; 2 << 1 = 4; 4 << 1 = 8; 8 << 2 = 32 |
左移一位相当于原值*2.
3.2 右移 >>
右移运算符>>将其左侧的操作数的值每位向右移动,移动的位数由其右侧的操作数指定。丢弃移出左侧操作数有段的位。对于unsigned类型,使用0填充左端空出的位。对于有符号类型,结果依赖于机器。空出的位可能用0填充,或者使用符号(最左端)位的副本填充。
//有符号值 (10001010) >> 2 (00100010) //在某些系统上的结果值 (10001010) >> 2 (11100010) //在另一些系统上的结果,看编译器 //无符号值 (10001010) >> 2 (00100010) //所有系统上的结果值 |
3.3 用法:移位运算符
移位运算符能够提供快捷、高效(依赖于硬件)对2的幂的乘法和除法。
number << n | number乘以2的n次幂 |
number >> n | 如果number非负,则用number除以2的n次幂,负数的话看编译器 |
边栏推荐
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
- U++ 原始内存 学习笔记
- Market Research - current market situation and future development trend of high tibial osteotomy plate
- UE4 游戏架构 学习笔记
- Pointer array parameter passing, pointer parameter passing
- U++ 学习笔记 ----松弛
- What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
- U++ learning notes - relaxation
- 数据库系统概论第一章简答题-期末考得怎么样?
- Unity3d learning notes 4 - create mesh advanced interface
猜你喜欢

牛客网:最大子矩阵

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

UE4 游戏架构 学习笔记

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!

Oracle-游标

SimpleITK使用——4. 奇怪的问题

Oracle PL / SQL programming

Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation

牛客网:龙与地下城游戏

wait解决僵尸进程
随机推荐
Phpcms realizes the direct Alipay payment function of orders
影视随摘
U++ learning notes - relaxation
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
NC24325 [USACO 2012 Mar S]Flowerpot
[foreign journal] sleep and weight loss
Mathematical modeling -- graph and network models and methods (I)
Pointer and string
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
Objects and object variables
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Market Research - current market situation and future development trend of high tibial osteotomy plate
Perceptron model and Application
建立自己的网站(22)
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
kubernetes 使用主机名将 pod 分配在指定节点上
杰理之修改不需要长按开机功能【篇】