2.3.7 Dividing by Powers of Two

本文深入探讨了如何利用逻辑右移操作来完成整数除以2的运算,并详细解释了不同类型的移位操作(逻辑与算术移位)在无符号数和补码数上的区别。文章还介绍了整数除法的基本规则及常见陷阱,通过实例展示了移位操作在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 .

image The examples show that the result of shifing consistently rounds toward zero, as is the convention for integer division.

 

image 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 12 时,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.

image

 

 

 

Suppose that , where .

 

 

 

when =0, (x+y-1)/y = k; when r>0. (x+y-1)/y=k+1.

转载于:https://www.cnblogs.com/baiweiguo/archive/2013/01/17/2864715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值