Current location - Training Enrollment Network - Mathematics courses - How to calculate the shift operation
How to calculate the shift operation
Shift operator is a bit operation operator in programming. The shift operator can shift numbers on a binary basis. According to the direction of translation and the rules of filling in numbers, there are three kinds:> (signed right shift) and >; & gt& gt (unsigned right shift).

Chinese name

Shift operator

meaning

Translate numbers on a binary basis.

for instance

& lt& lt (left shift operator)

belong to

Bitwise operator

quick

navigate by water/air

Left shift operator (

Right shift operator (>>) rule:

Unsigned right shift operator rule:

supplement

brief introduction

In c++, the shift operator has a binocular shift operator:> (move to the right). The expression composed of shift operators also belongs to arithmetic expression, and its value is arithmetic value. The left shift operation is to shift the operand of a binary bit to the left according to the specified number of bits, the shifted bit is discarded, and all spaces shifted to the right are filled with zeros. The right shift operation is to move the operand of a binary bit to the right according to the specified number of bits, the shifted bits are discarded, and all the spaces shifted to the left are filled with zeros or sign bits, depending on different machines. In a machine that uses the complement as the machine number, the sign bit of a positive number is 0 and the sign bit of a negative number is 1.

When shifting, the results of byte, short and char types will become int types. When shifting byte, short, char and int, if the compiler does not make any optimization (unpredictable after optimization), the actual number of shifts is specified as the remainder of the number of shifts and 32, that is, the result obtained by shifting 33 times is the same as that obtained by shifting 1 time. When moving a long value, the actual number of moves is defined as the remainder of the number of moves and 64, that is, the result obtained by moving 66 times is the same as that obtained by moving twice.

The movement rules and usage of the three shift operators are as follows:

Left shift operator (

Shift all the numbers to the left by the corresponding digits in binary form, shift them out (discard) at high places and fill in the blanks at low places.

Syntax format:

The number to shift

For example: 3

Calculation process:

3 & lt& lt2

Firstly, 3 is converted into a binary number 00000000000000000011,then two zeros in the upper (left) bit of this number are shifted out, and other bits are shifted two places to the left. Finally, two spaces in the lower (right) bit are filled with zeros. Then the final result is 000000000000000000000000001100, which is 12 in decimal.

Mathematical significance:

On the premise that the number does not overflow, whether it is positive or negative, moving one place to the left is equivalent to multiplying 2 by 1 power, and moving n places to the left is equivalent to multiplying 2 by n power.

Right shift operator (>>) rule:

In binary form, all numbers are shifted to the right according to the corresponding shift bits, and the high bits are filled with sign bits, that is, positive numbers are filled with 0 and negative numbers are filled with 1.

Syntax format:

Number to shift >; & gt number of shifts

Such as11>; & gt2, the number 1 1 is shifted 2 places to the right.

Calculation process:

The binary form of 1 1 is: 000000000000000101,and then the last two bits are shifted out. Because the digits are positive, the high bits are padded with zeros. Then the end result is 00000000000000000000000000000000000000000000010. Converted to decimal is 2.

Mathematical significance:

Moving 1 bit to the right is equivalent to dividing by 2, and moving n bits to the right is equivalent to dividing by 2 to the n power.

Unsigned right shift operator rule:

In the binary form, all numbers are shifted to the right by the corresponding number of digits, shifted out (discarded) at a low level, and padded with zeros at a high level. For positive numbers, the signed right shift is the same, but for negative numbers, it is different.

The article comes from Baidu Encyclopedia.