When it is positive, there is no problem.
But when it is negative, the result is correct for situations where rounding is not required. However, when the rounding is needed, the shift causes the result to be rounded down instead of rounding to 0 as the rule requires. For example, 7/2 should get-3 instead of-4.
Rule of Use: for integers x and any y > 0, there is "x/y = (x + y-1)/y". Round up, rounding down
Suppose X=ky+r, here 0≤r<y, gets (x+y-1)/y=k+ (r+y-1)/y, so (x+y-1)/y"=k+ (r+y-1)/y". When r=0, the following item equals 0, and when R > 0 o'clock, equals 1. That is, by adding a partial y-1 to X and then rounding the division down, when y divides x, we get k, otherwise we get K + 1. Therefore, for x < 0, if you precede the right shift with X plus (1<<k)-1, then we will get the result of rounding correctly.
Also that is: X is a negative number, Y/x should be equal to "x/y = (x + y-1)/y", but the right shift is actually rounded down ", so just equivalent to (x + y-1) Right shift
This analysis shows that for positive negative numbers using right shift instead of division, they can be represented as (x<0? x+ (1<<k)-1): x) >>k
Excerpt from: "In-depth understanding of computer systems" 2.3.7 divided by 2 power
Shift right instead of dividing the power by 2