int和unsigned int的区别- -

int和unsigned int的区别
1移位运算
为int生成的是算术移位指令SAL和SAR
为unsigned int生成的是逻辑移位指令SHL, SHR
对标志位的影响没有深入研究。
对结果的影响:
SHL, SHR都是直接左移,右移,SAL也是,SAL 0x80000000, 1 = 0
但SAR不是简单的移位,对正数是,负数就麻烦一些
SAR 0x80000000, 1 = 0XC0000000
就是对int右移是带符号的除2运算,但和一般的除法运算(IDIV)还不一样
如下
int i = -3;
数学上i/2=-1.5
用idiv做的话就会舍入到-1(向接近0的方向舍入),而用SAR来做会舍入到-2(向接近负无穷的方向舍入)
Intel的文档是这么讲的.
Using the SAR instruction to perform a division operation does not produce the same result as the IDIV instruction. The quotient from the IDIV instruction is rounded toward zero, whereas the “quotient” of the SAR instruction is rounded toward negative infinity. This difference is apparent only for negative numbers. For example, when the IDIV instruction is used to divide -9 by 4, the result is -2 with a remainder of -1. If the SAR instruction is used to shift -9 right by two bits, the result is -3 and the “remainder” is +3; however, the SAR instruction stores only the most significant bit of the remainder (in the CF flag).

所有有 -1 >> 1 == -1

2 比较运算
两个无符号数比较用jbe,...
Jump near if below or equal (CF=1 or ZF=1)
两个有符号数比较用jle...
Jump short if less or equal (ZF=1 or SF<>OF)
一个无符号数与一个有符号数比较用jbe
即把有符号数隐式转换为无符号数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值