Home > Common Problem > body text

How to understand the left shift and right shift operators in C language

清浅
Release: 2022-04-01 19:43:08
Original
56420 people have browsed it

Left shift in C language means left shift operator, which means discarding the highest bit and then adding 0 to the lowest bit; while right shift in C language means right shift operator, which is the opposite of left shift. It moves a few places to the right.

How to understand the left shift and right shift operators in C language

Usage of left shift and right shift operators in C language: left shift operator discards the highest bit and then fills the lowest bit with 0; right shift The algorithm is the opposite of left shift. It moves a few bits to the right.

The left shift and right shift operators in C language are operators in C language. Next, we will introduce them to you in detail in the article. How to use these two operators has certain reference value. I hope it will be helpful to everyone

[Recommended courses:C Language Tutorial

Let’s talk about left shift first. Left shift is to move all the bits of a number to the left by a certain number of bits. Use the << operator in C. For example:

int i = 1;
i = i << 2;  //把i里的值左移2位
Copy after login

That is to say, the binary system of 1 is 000...0001 (the number of 0s in front of 1 here is related to the number of digits in int. On a 32-bit machine, there are 31 in gcc 0), after shifting 2 bits to the left, it becomes 000...0100, which is 4 in decimal. Therefore, shifting 1 bit to the left is equivalent to multiplying by 2, then shifting n bits to the left is multiplying by 2 raised to the nth power. (Signed numbers are not fully applicable, because the left shift may cause the sign to change. The reason is explained below)
One problem that needs attention is that the sign bit of the m end is not reported on the leftmost bus of the int type and the shift is moved out Situation. We know that int is a signed integer, and the leftmost 1 bit is the sign bit, that is, 0 positive and 1 negative. Then overflow will occur when shifting, for example:

int i = 0x40000000; //16进制的40000000,为2进制的01000000...0000
i = i << 1;
Copy after login

Then, after i is shifted to the left by 1 bit, it will become 0x80000000, which is 100000...0000 in binary. The sign bit is set to 1, and the other bits are all 0, which becomes the minimum value that the int type can represent. , the value of 32-bit int is -2147483648, overflowing. What will happen if i is then shifted to the left by 1 bit? In C language, the highest bit is discarded. After discarding 1, the value of i becomes becomes 0.
A special case in left shift is that when the number of digits shifted to the left exceeds the maximum number of digits of the numerical type, the compiler will use the number of digits shifted left to modulo the maximum number of digits of the type, and then Shift according to the remainder, such as:

int i = 1, j = 0x80000000; //设int为32位
i = i << 33;   // 33 % 32 = 1 左移1位,i变成2
j = j << 33;   // 33 % 32 = 1 左移1位,j变成0,最高位被丢弃
Copy after login


When compiling this program with gcc, the compiler will give a warning, saying that the number of left shifts >= type length. Then In fact, i and j are moved by 1 bit, which is the remainder after 332. This is the rule under

gcc. It is not clear whether it is the same for other compilers.

In short, the left shift is: discard the highest bit and fill the lowest bit with 0

Let’s talk about right shift. If you understand the principle of left shift, then right shift will be easier to understand.

The concept of right shift and The opposite of left shift is to move a few bits to the right. The operator is > The sign bit will be kept unchanged, for example:

int i = 0x80000000;
i = i >> 1;  //i的值不会变成0x40000000,而会变成0xc0000000
Copy after login

That is to say, after the sign bit is moved to the right, 0 will be added for positive numbers and 1 for negative numbers, which is the arithmetic right shift in assembly language. Similarly When the number of bits moved exceeds the length of the type, the remainder is taken, and then the remainder is moved.

负数10100110 >>5(假设字长为8位),则得到的是  11111101
Copy after login

In short, in C, left shift is logical/arithmetic left shift (the two are exactly the same ), right shift is an arithmetic right shift, which will keep the sign bit unchanged. In actual applications, you can use left/right shift to perform fast multiplication/division operations according to the situation, which will be much more efficient than looping.

The above is the detailed content of How to understand the left shift and right shift operators in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template