当前位置:网站首页>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 |
边栏推荐
- 杰理之如何测试按键的误触率【篇】
- Socket socket c/s end process
- Oracle PL / SQL programming
- 杰理之修改不需要长按开机功能【篇】
- Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
- [LeetCode] 存在重复元素【217】
- 【板栗糖GIS】arcscene—如何做出有高度的高程图
- UE4 UI自适应屏幕
- 杰理之样机无触摸,拆机之后重新安装变正常【篇】
- Wait to solve the zombie process
猜你喜欢
[LeetCode] 多数元素【169】
数组进阶提高
世界环境日 | 周大福用心服务推动减碳环保
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Niuke: Dragon and dungeon games
加油站[问题分析->问题转换->贪心]
Graphic view frame
#include errors detected. Please update your includePath.
Oracle cursor
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
随机推荐
Source code analysis - lightweight asynchronous crawler framework Ruia
图形视图框架
[LeetCode] 反转字符串中的单词 III【557】
Simpleitk use - 4 Strange question
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Golang的学习路线
Phpcms realizes the direct Alipay payment function of orders
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
送给即将工作的自己
go 4种单例模式
Introduction to database system Chapter 1 short answer questions - how was the final exam?
Golang面试整理 三 简历如何书写
Mathematical modeling -- graph and network models and methods (I)
Utilisation de simpletk - 4. Question étrange
高并发介绍及应对
U++ 学习笔记 ----松弛
数组进阶提高
任务和特权级保护
位的高阶运算
【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?