[C language] Application of bit operators

Table of contents

The concept of bitwise operators:

1. & (Bitwise AND): If the binary bit corresponding to the complement of two integers is 0, it is 0, and if both are 1 at the same time, it is 1, and the obtained number is still the complement.

2. | (Bitwise OR): If the binary bit corresponding to the two's complement of two integers is 1, it is 1, and if both are 0 at the same time, it is 0, and the obtained number is still the complement.

3. ^ (bitwise XOR): The binary bits corresponding to the two’s complements of two integers are the same as 0, and the difference is 1, and the obtained number is still the complement.

Application of bitwise operators:

1. Application of & (bitwise AND):

2. Application of ^ (bitwise XOR):


The concept of bitwise operators:

There are three bitwise operators: & (bitwise and), | (bitwise or), ^ (bitwise exclusive or).

It should be noted that: the operand of the bit operator is an integer, and the specific implementation of the operation is the complement of two integers .


The concept of original code, complement code and inverse code:

(1) The original code, complement code, and inverse code of signed numbers are divided into two parts: the sign bit and the value bit. The sign bit is the first binary number, 0 means a positive number, and 1 means a negative number.

(2) The original code, complement code and inverse code of positive integers are the same .

(3) The original code of a negative integer is composed of the sign bit 1 and the corresponding value bit; the inverse code is that the sign bit of the original code remains unchanged, and the other bits are reversed bit by bit (0 becomes 1, 1 becomes 0); complement The code is inverse code +1.


1. & (Bitwise AND): If the binary bit corresponding to the complement of two integers is 0, it is 0, and if both are 1 at the same time, it is 1, and the obtained number is still the complement.

Take a chestnut:

//&(按位与)有0为0,同为1为1
int a = 1;//    补码为:00000000000000000000000000000001
int b = 2;//    补码为:00000000000000000000000000000010
int c = a & b;//补码为:00000000000000000000000000000000
//所以c的值为0

2. | (Bitwise OR): If the binary bit corresponding to the two's complement of two integers is 1, it is 1, and if both are 0 at the same time, it is 0, and the obtained number is still the complement.

Take a chestnut:

//|(按位或)有1为1,同为0为0
int a = 1;//    补码为:00000000000000000000000000000001
int b = 2;//    补码为:00000000000000000000000000000010
int c = a | b;//补码为:00000000000000000000000000000011
//所以c的值为3

3. ^ (bitwise XOR): The binary bits corresponding to the two’s complements of two integers are the same as 0, and the difference is 1, and the obtained number is still the complement.

Take a chestnut:

//^(按位异或)相同为0,相异为1
int a = 1;//    补码为:00000000000000000000000000000001
int b = 2;//    补码为:00000000000000000000000000000010
int c = a ^ b;//补码为:00000000000000000000000000000011
//所以c的值为3

Application of bitwise operators:

1. Application of & (bitwise AND):

(1) Take the end of a certain binary number:

int a = 1;//补码:00000000000000000000000000000001
int tmp1 = a & 1;//此时tmp1的值为1
int b = 2;//补码:00000000000000000000000000000010
int tmp2 = a & 1;//此时tmp2的值为0

For example, some topics require you to output the binary digits of a certain number bit by bit, then you can use the shift operator to achieve:

int main()
{
	int a = 1;
	int i = 0;
	for (i = 0; i < 32; i++)
	{
		printf("%d", a & 1);
		a >>= 1;
	}
	return 0;
}

(2) Make the rightmost 1 of a certain binary number disappear:

int n = 1;//n的补码:00000000000000000000000000000001
n = n & (n - 1);(n-1)的补码:00000000000000000000000000000000
//结果n的补码:00000000000000000000000000000000

2. Application of ^ (bitwise XOR):

An interview question: Swap two numbers without creating a temporary variable.

Generally, if you want to exchange two numbers, you can do this:

tmp = num2;
num2 = num1;
num1 = tmp;

If you create a temporary variable, there is actually another way:

num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;

So how to use ^ (bitwise XOR) to realize variable exchange, in fact, the operation rule of ^ (bitwise XOR) satisfies the exchange law, for example:

a ^ a = 0;
a ^ 0 = a;
a ^ b ^ a = b;//先运算a ^ a = 0;在运算0 ^ b = b;
//可以理解为(a ^ a) ^ b = b;

From this we can know:

a = a ^ b;
b = a ^ b;
a = a ^ b;

Draw a picture:

This method is very clever, but it is not very practical, and its efficiency is not even as high as creating temporary variables, but it can give you a better understanding of bit operators that may achieve some zero operations that you did not expect.

Guess you like

Origin blog.csdn.net/2301_77112634/article/details/130899885