C language programming skills---Comparison of left shift, right shift and multiplication and division in C language

Comparison of right shift and division in C language

        When working on a project recently, I encountered an interesting phenomenon. That is, for the operation of dividing by an integer power of 2, in order to speed up the calculation, in general, the right shift (>>) will be used instead of the division (/). But in fact, in VS, shifting right is equivalent to the floor (floor) operation in matlab, that is, rounding towards negative infinity .

Part I: Division and right shift

First look at the case where the dividend is negative:

1,-3/2 = -1.5

For division, -3/2 is equal to -1, which is equivalent to directly truncating decimal places without rounding.

For right shifting, -3>>1 equals -2, possibly with rounding (-0.5), possibly with rounding towards minus infinity -Inf, but without truncation.

2,-7/4 = -1.75

For division, -7/4 is equal to -1, and again, the decimal places are truncated directly, without rounding.

For right shifting, -7>>2 equals -2, possibly with rounding (-0.75), possibly with rounding towards minus infinity -Inf, but without truncation.

3,-5/4 = -1.25

For division, -5/4 is equal to -1, which is directly truncated.

For the right shift, -5>>2 is equal to -2, which proves that the previous calculation is not rounded (-0.25), and it must be rounded towards negative infinity -Inf, which is equivalent to the floor in matlab.

Summary: For the case where the dividend is negative, right shifting is not equivalent to division. Division directly truncates the decimal part, while right shifting is floor.

When the dividend is a positive number:

1,3/2 = 1.5

For division, 3/2 is equal to 1, which is the same as the result of the dividend being a negative number. The only difference is the sign, which is still truncated.

For right shifting, 3>>1 is equal to 1, which is different from the result of a negative dividend. From 1.5 to 1, it proves once again that the method of shifting to the right is rounding towards negative infinity-Inf.

2,7/4 = 1.75

For division, 7/4 is equal to 1, which is directly truncated. Same result as for a negative dividend, except for a different sign.

For right shifting, 7>>2 equals 2, from 1.75 to 1, rounded towards negative infinity -Inf.

3,5/4 = 1.25

For division, 5/4 is equal to 1, directly truncated, and the result is the same as the dividend is negative.

For right shifting, -5>>2 is equal to -2, from 1.25 to 1, which is equivalent to floor, rounded towards negative infinity -Inf.

Summary: For the case where the dividend is positive, the result of right shift is equal to division. For positive numbers, the effect of direct truncation by division is equivalent to rounding towards -inf.

Summarize:

        For division, the result is the same whether the dividend is positive or negative. For right shift, the result is different when the dividend is positive and negative.

Part II: Multiplication and Left Shift

        Because there are no decimals in the calculation of multiplication, the results of left shift and multiplication are the same for positive and negative numbers.


 (full text)

Author --- Panasonic J27

 (The accompanying picture has nothing to do with this article)

Copyright statement: Some pictures, texts or other materials in this article may come from many different websites and descriptions, so I cannot list them here. If there is any infringement, please let me know and delete it immediately. Everyone is welcome to reprint, but if someone quotes or copies my article, you must indicate in your article that the pictures or text you use come from my article, otherwise, the infringement will be investigated. ----Panasonic J27
 

Guess you like

Origin blog.csdn.net/daduzimama/article/details/130382263