当前位置:网站首页>Higher order operation of bits
Higher order operation of bits
2022-07-02 22:52:00 【Soy sauce;】
1. Concept
have access to C Operate on individual bits in a variable . You may wonder why people want to do this . This ability is sometimes really necessary , Or at least useful .C Provide bit logical operators and shift operators . In the following example , We'll write out the value using binary counting , So that you can understand what happens to the bit . In a real program , You can use integer variables or constants in the general form . For example, not applicable 00011001 In the form of , And write as 25 perhaps 031 perhaps 0x19. In our case , We will use 8 Digit number , From left to right , The number of each is 7 To 0.
2. Bitwise logical operator
4 Bitwise operators are used for integer data , Include char. The reason these bit operators are bit operations is that they operate on each , Without affecting the positions on the left and right sides . Please do not compare these operators with regular logical operators (&& 、|| and !) To confuse , Regular bit logical operators operate on the entire value .
2.1 According to the not ~
Unary operator ~ Each one 1 Turn into 0, Each one 0 Turn into 1, Like the following example :
~(10011010) 01100101 |
hypothesis a It's a unsigned char, Assigned to 2. In binary ,2 yes 00000010. therefore -a The value of is 11111101 perhaps 253. Note that this operator does not change a Value ,a Still 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 Bit and (AND): &
Binary operator & By comparing the two operands bit by bit, a new value . For each bit , Only the corresponding bits of the two operands are 1 The result is 1.
(10010011) & (00111101) = (00010001) |
C There is also a combined bit and - Assignment operator :&=. The following two will produce the same result :
val &= 0377 val = val & 0377 |
2.3 Bit or (OR): |
Binary operator | By comparing the two operands bit by bit, a new value . For each bit , If the corresponding bit in any operand is 1, Then the result bit is 1.
(10010011) | (00111101) = (10111111) |
C There are also combinatorial bits or - Assignment operator : |=
val |= 0377 val = val | 0377 |
2.4 Bit exclusive or :
Binary operator ^ Compare the two operands bit by bit . For each bit , If one of the corresponding bits in the operand is 1( But not all 1), So the result is 1. If it's all 0 Or both 1, Result bit 0.
(10010011) ^ (00111101) = (10101110) |
C There is also a combinatorial exclusive or - Assignment operator : ^=
val ^= 0377 val = val ^ 0377 |
2.5 usage
1 Open bit (|) Set one
It is known that :10011010:
- Will bit 2 open
flag | 10011010
(10011010) |(00000100) =(10011110) |
- Turn all bits on .
flag | ~flag
(10011010) |(01100101) =(11111111) |
2 Off bit (&) Zeroing
flag & ~flag
(10011010) &(01100101) =(00000000) |
3 Transpose (^) Take the opposite
Transposition (toggling) A bit indicates if the bit is on , Then close this bit ; If this bit is off , Then open . You can use the bitwise exclusive or operator to transpose . The idea is if b It's a bit (1 or 0), So if b by 1 be b^1 by 0, If b by 0, be 1^b by 1. No matter what b The value of is 0 still 1,0^b by b.
flag ^ 0xff
(10010011) ^(11111111) =(01101100) |
4 Swapping two numbers does not require temporary variables
//a ^ b = temp; //a ^ temp = b; //b ^ temp = a (10010011) ^(00100110) =(10110101) (10110101) ^(00100110) 10010011
int a = 10; int b = 30; |
5 Judge parity (&)


3. Shift Operators
Now let's take a look at C The shift operator for . The shift operator shifts bits to the left or right . Again , We will still explicitly use binary form to illustrate how the mechanism works .
3.1 Move left <<
Shift left operator << Moves each of the values of its left operand to the left , The number of bits to move is specified by its right operand . The empty space is used 0 fill , And discard the bits that move out of the end of the left operand . In the following example , Each person moves two positions to the left .
(10001010) << 2 (00101000) |
This operation will result in a new location , But do not change its operands .
1 << 1 = 2; 2 << 1 = 4; 4 << 1 = 8; 8 << 2 = 32 |
One bit to the left is equivalent to the original value *2.
3.2 Move right >>
Shift right operator >> Moves the value of each operand to its left to the right , The number of bits to move is specified by the operand to its right . Discards bits that have segments removed from the left operand . about unsigned type , Use 0 Fill the empty bit at the left end . For signed types , The result depends on the machine . The vacant space may be used 0 fill , Or use symbols ( Leftmost end ) Bit copy fill .
// There are signed values (10001010) >> 2 (00100010) // Result values on some systems (10001010) >> 2 (11100010) // On other systems result , Look at the compiler // Unsigned value (10001010) >> 2 (00100010) // Result values on all systems |
3.3 usage : Shift Operators
The shift operator provides a shortcut 、 Efficient ( Depending on the hardware ) Yes 2 Multiplication and division of powers of .
number << n | number multiply 2 Of n The next power |
number >> n | If number non-negative , Then use number Divide 2 Of n The next power , If the number is negative, look at the compiler |
边栏推荐
猜你喜欢

图形视图框架

Dynamic memory allocation (malloc calloc realloc free)

Source code analysis - lightweight asynchronous crawler framework Ruia

NC50965 Largest Rectangle in a Histogram
![Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)](/img/b7/0f543829b57cf2f2544efec4910c17.png)
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)

NC50965 Largest Rectangle in a Histogram

Phpcms realizes the direct Alipay payment function of orders
![[foreign journal] sleep and weight loss](/img/81/42dcfae19e72a0bc761cb7a40fe5d5.jpg)
[foreign journal] sleep and weight loss

PMP项目整合管理

悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
随机推荐
【板栗糖GIS】arcscene—如何做出有高度的高程图
Market Research - current situation and future development trend of herringbone gear Market
Utilisation de simpletk - 4. Question étrange
Dynamic memory allocation (malloc calloc realloc free)
高并发介绍及应对
小鹏P7出事故,安全气囊未弹出,这正常吗?
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
[LeetCode] 反转字符串中的单词 III【557】
百度智能云-创建人脸识别应用
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Graphic view frame
Go condition variable
【外刊】睡眠与减肥
电商系统微服务架构
go 条件变量
原生js添加样式的方法
全面解析分享购商业模式逻辑?分享购是如何赋能企业
JS solution for obtaining the width and height of hidden elements whose display is none