Bitwise shift operators are not divided. They do what they have to do - shift bits. In particular, the right shift operator does the following:
- for each bit starting on the right, set its value to what is on its left
- for the leftmost bit that has nothing left, save the current value
For example, if your number
1011...101
right shift gives you
11011...10
So, the rightmost bit (LSB) is lost, and the leftmost bit (MSB) is duplicated. This is called "sign propagation" because the MSB is used to extract positive (MSB = 0) from negative (MSB = 1) numbers.
Negative numbers are stored as "two additions", that is, in a 32-bit system, -x stored as 2^32-x . So, -1 - 10...00 (32 zeroes) - 1 == 1...1 (32 ones) . If you move 32 in accordance with the above procedure, you will again get 32, i.e. -1 >> whatever will always be -1 .
The difference between the right shift and division by two is that the shift gives the same results for odd and even numbers. Since the rightmost bit is lost when you shift an odd number (which has LSB = 1), the result will be the same as the shift of the next lower even number (the same bit combination, but with LSB = 0). Thus, you do not get halves during the shift, as the dividend is forced to be even. For example,
10 10 = 1010 2 , 10/2 = 5.0 and 10 โ 1 == 5 10 == 101 2sub>
11 10 = 1011 2 , 11/2 = 5.5, but 11 โ 1 == 5 10 == 101 2sub>
If you prefer to think of x >> 1 in terms of division, he first โroundsโ x to an even number ( x - abs(x) % 2 ), and then divides this number by two.
For x = -1 this gives you (-1 - abs(-1) % 2)/2 == (-1 - 1)/2 = -2/2 = -1 .
georg
source share