Assuming 2-padding, just a dividend bit-shift is equivalent to a certain type of division: non-conditional division, where we round the dividend to the next multiple divisor to zero. But another view, where we turn the dividend to negative infinity. I rediscovered this in Smalltalk, see http://smallissimo.blogspot.fr/2015/03/is-bitshift-equivalent-to-division-in.html .
For example, let them divide -126 by 8. Traditionally, we will write
-126 = -15 * 8 - 6
But if we round to infinity, we get a positive remainder and write it:
-126 = -16 * 8 + 2
A bit shift performs a second operation with respect to bit patterns (assuming 8 bits of int length for short):
1000|0010 >> 3 = 1111|0000 1000|0010 = 1111|0000 * 0000|1000 + 0000|0010
So, what if we want the traditional division with rounding to be rounded to zero and remain the same sign as a dividend? Simply, we just need to add 1 to the quotient - if and only if the dividend is negative and the division is inaccurate.
You saw that x>>31 matches the first condition, the dividend is negative if int has 32 bits.
The second term corresponds to the second condition if the division is inaccurate.
See how -1, -2, -4, ... are encoded in two additions: 1111 | 1111, 1111 | 1110, 1111 | 1100. Thus, negation of the nth power of two has n finite zeros.
When a dividend has n finite zeros, and we divide by 2 ^ n, then there is no need to add 1 to the final quotient. In any other case, we need to add 1.
Which ((1 <n) + ~ 0) makes creating a mask with n finite.
The last n bits do not matter much because we are going to move to the right and just throw them away. So, if the division is accurate, the n final bits of the dividends are zero, and we just add n 1s to be skipped. Conversely, if the division is inaccurate, then one or more of the n final bits of the dividend is equal to 1, and we will necessarily cause the transfer to the position n + 1 bits: how do we add 1 to the quotient (we add 2 ^ n to the dividend). Does this explain a little more?