Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Basic information of right shift operator
Basic information of right shift operator
One of the shift operators:

Displacement operator in c language

Displacement bit operator

Shift operator is an operation that treats data as a binary number and moves it to the left or right by several bits. Displacement operators can be divided into left shift and right shift, both of which are binocular operators. The first operand is the shifted object, and the second operand is the shifted binary number.

The operands, operation rules, results and combinations of the displacement operator are shown in Table 2- 16.

When shifting, all the shifted digits are discarded, and the number of shifting vacancies is related to the left or right shift. If it is left shift, it is stipulated that all the added numbers are 0; If it is shifted to the right, it is also related to whether the shifted data has a sign. If there are unsigned numbers, all the added numbers are 0; If it is a signed number, all the added numbers are equal to the original number of the leftmost bit of the original number (that is, the original sign bit). The specific shifting rules are as follows.

The priority of shift operators is as follows:

Arithmetic operators take precedence over displacement operators and relational operators.

The displacement operator is first-order, and the combination is from left to right.

For example, let the unsigned short integer variable A be 011(the corresponding binary number is 0000000 10065438).

Then: the result of a <<3 is 0110 (the corresponding binary number is 000001001000), and a remains unchanged.

The result of a>& gt4 is 04 (the corresponding binary number is 000000000100), and a remains unchanged.

For another example, let the short integer variable A be -4 (the corresponding binary number is11111165438).

Then: a < < the result is -32 (the corresponding binary number is11111/kloc-0.

The result of a>& gt is-1 (the corresponding binary number is1111116544.

Left shift and right shift operators in C language

Let's talk about moving left first. Shifting to the left means shifting all the bits of a number to the left by several bits and using the

int I = 1;

i = i & lt& lt2; //Shift the value in I by 2 bits to the left.

That is to say, the binary of 1 is 000...000 1 (here, the number of zeros in front of 1 is related to the number of bits of int, and there are 3 1 zeros in 32-bit gcc), and it is changed to 01000 after 00 .. shifted to the left by two digits.

One problem that needs to be paid attention to is the leftmost sign bit of int type and the situation of moving out. As we know, int is a signed integer, and the leftmost 1 bit is a signed bit, that is, 0 is positive 1 negative, so there will be overflow when shifting, for example:

Int i = 0x40000000// hexadecimal 1000000, binary 0 1000000. ...

i = i & lt& lt 1;

Then, if I shift the 1 bit to the left, it will become 0x80000000, that is, 100000...0000 binary, the sign bit is set to 1, and all other bits are zero, which becomes the minimum value that can be represented by the int type. The value of 32-bit int is -2 147488. In C language, most significant bit is discarded. When 1 is discarded, the value of I becomes 0.

A special case of left shift is that when the number of digits left shift exceeds the maximum number of digits of numerical type, the compiler will use the number of digits left shift to remove the maximum number of digits of the model, and then shift according to the remainder, for example:

Int i = 1, j = 0x80000000// Let int be 32 bits.

i = i & lt& lt33; // 33% 32 = 1 shift left 1, and I becomes 2.

j = j & lt& lt33; // 33% 32 = 1 shift left 1 bit, j becomes 0, and the highest bit is discarded.

When compiling this program with gcc, the compiler will give a warning, saying shift the number of digits left >;; = Type length. So in fact, J and I moved 1 bit, which is the remainder after 33%32. This is a rule under gcc, and it is not clear whether other compilers are the same.

In short, moving to the left means discarding the most significant bit and filling the least significant bit with 0.

Let's talk about moving to the right first and understand the reasons for moving to the left, then moving to the right is easier to understand.

The concept of moving right is the opposite of moving left, that is, moving a few places to the right, and the operator is >>.

Shift right and shift left handle sign bits in different ways. For signed integers, such as int type, shifting to the right will keep the sign bit unchanged, for example:

int i = 0x80000000

i = i & gt& gt 1; The value of //i will not become 0x40000000, but 0xc00000000.

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

Negative number10100110 >; 5 (assuming the word length is 8 bits), you get11111.

In short, in C, the left shift is a logical/arithmetic left shift (both are exactly the same), and the right shift is an arithmetic right shift, which will keep the sign bit unchanged. In practical application, we can use left/right shift to do fast multiplication/Divison according to the situation, which is much more efficient than loop.

In many system programs, it is often necessary to perform operations or processes at the bit level. C language provides the function of bit operation, which makes C language can also be used to write system programs like assembly language.