Dividing by a power of 2 can be performed using a right shift operation.
The two different shifts-logical and arithmetic-serve the shift operation for unsigned and two's-complement numbers, respectively.
Integer division always rounds toward zero.
1.Consider unsigned number for logical right shift by k. We claim this gives the same result as dividing by .
The examples show that the result of shifing consistently rounds toward zero, as is the convention for integer division.
Compared with figure 2.27 and figure 2.28, when k=0 and 1, , while when k = 4 or 8, .
U2B(12340):
0011000000110100
The negation of 12340 is:
1100111111001100
右移操作符:>>k
当 k = 0, 1,2 时,U2B(12340) + T2B(12340) = 0000000000000000 = 0
当 k >= 3 时, U2B(12340) + T2B(12340) = 1111111111111111 =–1
Figure 2.28 show that the shifing is not consistent with dividing by , the following method correct this improper rounding by “biasing”
. The result show in figure 2.29.
Suppose that , where .
when =0, (x+y-1)/y = k; when r>0. (x+y-1)/y=k+1.