Detailed explanation of shift operators and bit operators in C language

content

1. Shift operator

2. Bit operator


1. Shift operator

It is divided into left shift operator (<<) and right shift operator (>>).

Left shift operator:

Shifting rules: discard on the left and add 0 on the right.

What do you mean, let me explain it in detail now.

For example, we define an integer variable a and assign it an initial value of 5. For example, int a=5; then move a to the left, that is, a<<2, what is the result? Before speaking, we must first understand how the integer 5 is stored in the memory. For those who are not sure, please see my previous blog post on the three expressions of integers and their storage in memory . There are detailed explanations. The integer 5 is stored in two's complement form. Shifting 5 to the left by 2 bits means shifting the binary bits stored in memory by 2 bits to the left, discarding the 2 bits, and padding the right side with 0s. The final result is the result after 5<<2, which can be briefly described with the following picture~

 According to the above figure, the result obtained after a<<2 is 20.

Right shift operator:

First, there are two types of right shift operations. Arithmetic shifts and logical shifts.

Arithmetic shift rule: The left side is filled with the sign bit of the value, and the right side is discarded.

Logical shift rule: the left is supplemented with 0, and the right is discarded.

What does it mean specifically, let's take a look at a few examples, such as shifting 5 to the right, that is, 5 >> 1, how much do you get?

If it is an arithmetic shift, it is to move the binary bit stored in memory by 5 to the right by 1 bit and discard this bit, and fill the left side with the sign bit of the binary bit stored in the value. As shown below:

So the result of 5>>1 is 2. What about the logical shift, the logical shift is to use 0 on the left, supplement, and discard on the right. For positive numbers, the sign bit is 0, so the result of arithmetic shift and logical shift is the same. Logical shift can also be represented by the above figure, and the result is also 2.

If you shift -5 to the right one place, what is the result? Let's take a look at the arithmetic shift first, so I won't go into details. Please see the following figure:

So -5>>1 arithmetic shift gets -3.

In the same way, let's take a look at what is obtained by -5>>1 logical shift~

Because the highest bit is 0, the final result is 3~. But in general, arithmetic shifts are used.

2. Bit operator

& bitwise (binary) AND

| Bitwise (binary bits) or

^ Bitwise (binary) XOR

Let's look at this code directly. You can first guess which three numbers are output at the end:

int main()
{
	int a = 3;
	int b = -5;
	printf("%d %d %d", a & b, a | b, a ^ b);
	return 0;
}

Let's analyze it first: bitwise AND is to compare the binary bits stored in two numbers, and if both are 1, it is 1, otherwise it is 0. Therefore, a&b can be analyzed with a graph as:

So the result of a&b is 3.

Bitwise OR is to compare the binary bits stored in two numbers, if one of the two is 1, it is 1, otherwise it is 0. Therefore, a|b can be analyzed with a graph as:

So the value of a|b is -5.

The bitwise XOR is to compare the binary bits stored in two numbers. The same is 0, and the difference is 1. Therefore, a^b is analyzed by a graph:

 So the result of a^b is -8.

Finally, let's run the program:

 Is it the same as the analysis?

It's over here, I hope you can give a like~~~

Guess you like

Origin blog.csdn.net/m0_63039919/article/details/121494424