Bit operations in C language

Bit operation concept: bit operation is an operation that directly operates on binary digits.
The bit operation of C language is a major feature that distinguishes C language from other high-level languages. Using this function, C language can realize some low-level operations, such as hardware programming or system calls.
Note: When performing bit operations, the data object can only be an integer number (int, short int, unsigned int, long int) or a character number, and cannot be some other data types, such as single precision or double precision.
The C language contains 6 kinds of bit operators, as shown in the figure below.
insert image description here
Only the bitwise inversion operator "~" is a unary operator, and the others are binocular operators.
Order of precedence:
The bitwise negation operator "~" has a higher priority than arithmetic operators and relational operators, and is the highest priority among all bitwise operators. Followed by the left shift "<<" and right shift ">>" operators, the priority of these two operators is higher than that of relational operators, but lower than that of arithmetic operators, bitwise AND "&" , bitwise or "|" and bitwise exclusive or "^" are lower than the precedence of arithmetic operators and relational operators.

Bitwise AND (&)

The function of the bitwise AND operation is to perform the "AND" operation on the two operands participating in the operation according to the corresponding binary bits. Only when the corresponding two binary bits are both 1, the result bit is 1, otherwise it is 0. The numbers involved in the operation appear in the form of two's complement. For example, the result of 9&8 is
9: 0 0 0 0 1 0 0 1
8: 0 0 0 0 1 0 0 0
———————
8: 0 0 0 0 1 0 0 0
The bitwise AND operation is usually used to Clear certain bits or reserve bits. For example, the value of operand a is 1001 1010 0010 1011, the upper 8 bits of this number should be cleared, and the lower 8 bits should be reserved. The solution is to calculate with 0000 0000 1111 1111. The result after the operation is 0000 0000 0010 1011, the upper 8 bits of the operand are all 0, and the lower 8 bits are the same as before.

Bitwise OR (|)

The operation rule of bitwise or is: the binary phase or corresponding to the two numbers participating in the operation. As long as one of the corresponding binary bits is 1, the result bit is 1. Participating numbers all appear in the form of two's complement. For example:
0 0 1 0 0 1 1 0
0 0 0 1 1 0 1 1
———————
0 0 1 1 1 1 1 1
According to the characteristics of the "|" operation, it can be used to set certain positions of the data to 1 , as long as the binary number on the bit to be set is 1 and the other bits are 0, the "|" operation can be performed.

Bitwise negation operation (~)

Bitwise inversion operation is also called bitwise NOT operation, which is to invert every bit of the operand (that is, 1 becomes 0, 0 becomes 1). It is the only unary operator among bitwise operators. For example: 0101 0011 is reversed to 1010 1100

Bitwise XOR operation (^)

The operation rule of the bitwise XOR operation is: if the corresponding binary bits in the two operands participating in the operation are the same, the result bit is 0, and if they are different, the result bit is 1. For example,
0 0 1 0 1 1 0 1
0 1 1 0 0 1 1 0
———————
0 1 0 0 1 0 0
The result of XOR with 0 is still itself, and the result of XOR with 1 is equivalent to the original The number is bitwise inverted. Utilizing this characteristic , several bits of an operand can be flipped, as long as it is XORed with another operand whose corresponding bit is 1 and the remaining bits are 0.
XOR can also realize the exchange of two numbers, for example:

#include <stdio.h>
void main()
{
    
    
	int a,b;
	a=56,b=37;
	a=a^b;
	b=b^a;
	a=a^b;
	printf("a=%d,b=%d\n",a,b); 
}

The output is a=37, b=56. The change operation does not use intermediate variables to realize the exchange of ab, which is also a usage of XOR.

Left shift operation (<<)

The rule of the left shift operation is: shift all the binary bits of the operand on the left of "<<" to the left by a certain number of bits, and specify the number of digits shifted by the number on the right of "<<", discard the high bits, and add 0 to the low bits. For example:
x : 0000 0101 (x=5)
y=x<<1 :0000 1010 (y=10)
z=x<<2 :0001 0100 (z=20)

It can be seen that shifting one bit to the left is equivalent to multiplying the original number by 2, and shifting n bits to the left is equivalent to multiplying the original number by 2^n, where n is the number of digits to be moved. In actual operation, the left shift operation is much faster than the multiplication operation, so the left shift operation is often used instead of the multiplication operation.
**Note: When the left shift operation replaces the multiplication operation, if the left shifted part contains the binary number 1, the feature does not apply. **Example:
x : 0100 0110 (x=70)
y=x<<1 :1000 1100 (y=140)
z=x<<2 :0001 1000 (z=24)

Right shift operation (>>)

The function of the right shift operation is to move all the binary bits of the operand on the left of ">>" to the right, and the number on the right of ">>" determines the number of bits to move. The padding at the left end is divided into two cases: if the number is an unsigned number or a positive integer, the high bit is filled with 0; if the number is a negative integer, the high bit is filled with 0 or 1, depending on the compilation system. For example
a : 0000 1011
b=a>>2 : 0000 0010

Summarize

Bit operation is a major feature of C language, and some low-level operations can be realized through bit operation. In microcontroller programming, bit operations in C language are often used. Be familiar with the rules of each bit operation and the functions it can realize.
"&" operation: usually used to clear certain bits or reserve bits
"|" operation: can be used to set certain positions of data "
^" operation: can realize the flipping of several bits of an operand, and can also be used to Realize the exchange
"<<" operation of two numbers: it can be used instead of multiplication, but pay attention to its conditions

One more thing to distinguish is "&&" and "&", "||" and "|". Do not confuse logical and with bit-and, logical or and bit-or. Pay attention to the way of complementing the high bit in the ">>" operation.
If the lengths of the numbers involved in the operation are different, at this time, the two numbers can be operated according to the right end, and the high-order filling method is: if the shorter number is a positive number or an unsigned number, its high-order number will be filled with 0; if it is shorter If the number is negative, the high bit is filled with 1.

Guess you like

Origin blog.csdn.net/Tao_9/article/details/129961428