For example:

`printf("%d\n" , 10 << 3);`

I know how to deal with this small displacement, 10 converted to the original code, and then add 3 0 later, in the conversion to the decimal system, that is the result.

But:

`printf("%d\n" , 10 << 31)`

`10`

The binary representation of`1010`

，The length is 4. If`10 >> 31`

，If the normal complement is 31 0, then the length is`31 + 4 = 35`

Position, obviously more than`intType 10`

Maximum number of digits`32bit`

。`c`

How do you deal with this displacement?

CLogical displacements and arithmetical displacements in language standards

In the C language, there are 2 operators involving displacement.

`>>`

To move to the right,`<<`

It indicates left shift.

And in the assembly instruction,`SHL`

and`SHR`

It represents the logic left shift and the logical right shift,`SAR`

and`SAL`

It represents the arithmetic left and the arithmetic right shift.

Among them, the left shift of logic and the left shift of arithmetic are the shift of register binary digit to the left and the complement of 0 on the right side.

While the right shift is different, the logical right shift is the overall shift to the right, and the 0 on the left side, while the arithmetic shift to right is based on the value of the original symbol bit and its same value.So how do we implement logical and arithmetic shifts in C language? According to the C standard, if the variables on the left side of the displacement operator are signed numbers, such as

`int`

,`char`

,`short`

The compilation instructions generated by the compiler are arithmetic shift instructions, if the variable is unsigned.`unsigned int`

,`unsigned char`

The compilation instructions generated by the compiler are logical displacement instructions.

Shift instruction

`SAL(shift arithmetic left)`

Arithmetic left shift

Format:`SAL`

,`OPR`

,`CNT`

Meaning: arithmetic left shift`SAL`

To shift the low position of the target operand to the high position and to vacate the low bit complement 0..`62H`

by`01100010B`

，The displacement is only in ordinary understanding to move the binary bits to the left or to the right, and the number of moves is determined by the operator to abandon or put the vacant position, the vacant position is determined by the operator for 0 or the number of the moved.

As you can know, the underlying execution`10 << 31`

At that time, the part beyond the length will be discarded and then added to the original space 0.