当前位置:网站首页>[CSAPP Practice Problem 2.32] tsub_ok(int x, int y)判断补码减法是否溢出
[CSAPP Practice Problem 2.32] tsub_ok(int x, int y)判断补码减法是否溢出
2022-07-25 19:29:00 【leehyukshuai】
Practice Problem 2.32:
You are assigned the task of writing code for a function tsub_ok, with arguments x and y, that will return 1 if computing x-y does not cause overflow. Having just written the code for Problem 2.30, you write the following:
/* Determine whether arguments can be subtracted without overflow */ /* WARNING: This code is buggy. */ int tsub_ok(int x, int y) { return tadd_ok(x, -y); }For what values of x and y will this function give incorrect results? Writing a correct version of this function is left as an exercise.
首先,要弄清楚几个概念:
- 溢出:是指计算机计算后得出的结果与单纯的算数运算所得结果不同。(由于产生这种现象的原因是某类型的位数(size)不足以容纳结果,所以叫做溢出。)
- 补码的减法:计算机内部是没有减法单元的,所以减法的运算实际上是:x-y=x+(-y),仍然是套用加法,不过是把减数的正负反转了一下。
- 补码的负:当x为最小值时,其负值仍为自身,其他情况下结果为-x。
因此说,当y不等于INT_MIN时,结果就应当是tadd_ok(x, -y);而当y恰好等于INT_MIN时,由于-y的结果仍为INT_MIN,所以还需要再讨论一下。
当y==INT_MIN时:(为方便讨论,假设int只有有4bit大小。)
- 若x为非负数,举个例子:x=[0010]->2,y=[1000]->-8,记计算机计算所得结果为a,纯算数运算结果为b。则-y=[1000]->-8,a=x-y=x+(-y)=[0010]+[1000]=[1010]->-6,b=x-y=10。a!=b,因此发生了溢出。
- 若x为负数,同样举个例子:x=[1111]->-1,y=[1000]->-8,记计算机计算所得结果为a,纯算数运算结果为b。则-y=[1000]->-8,a=x-y=x+(-y)=[1111]+[1000]=[0111]->7,b=x-y=7。a==b,因此没有发生溢出。
也就是说,正确的tsub_ok函数,应该是这样子写的:
int tsub_ok(int x, int y)
{
if (y == INT_MIN) {
if (x >= 0) {
return 0;
} else {
return 1;
}
} else {
return tadd_ok(x, -y);
}
}以下就是官方答案:
Solution to Problem 2.32
This function will give correct values, except when y is TMin. In this case, we will have -y also equal to TMin, and so the call to function tadd_ok will indicate overflow when x is negative and no overflow when x is nonnegative. In fact, the opposite is true: tsub_ok(x, TMin) should yield 0 when x is negative and 1 when it is nonnegative.
One lesson to be learned from this exercise is that TMin should be included as one of the cases in any test procedure for a function.
斜体字部分似乎有问题,我查的中文版翻译是:当x为负数时,tsub_ok(x, TMin)应该为1,当x为非负数时,tsub_ok(x, TMin)应该为0。但这句话对应的中文意思好像是:当x为负数时,tsub_ok(x, TMin)应该为0,当x为非负数时,tsub_ok(x, TMin)应该为1。
不清楚是是这一句英文有问题还是我英语水平的问题。
边栏推荐
- 基于PHP的中非南南合作信息交流平台网站建设
- Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work
- 鸿蒙-大喵计算画板-视频
- Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report
- FPGA based 1080p 60Hz bt1120 interface debugging process record
- 浅谈接口加密
- leetcode刷题:动态规划07(不同的二叉搜索树)
- 485 current acquisition module dam-8041
- Wxss template style and WXS scripting language for wechat applet development
- Leetcode skimming: dynamic programming 07 (different binary search trees)
猜你喜欢

二叉树可视化

binarySearch基础二分查找
![[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection](/img/22/426bcb8641db5bfe07e8aacf5e8427.png)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection

Use of swift basic codable (jsonencoder jsondecoder)

微信小程序 26 播放音乐页的完善②

Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt

What is the application value of MES management system

解决Win10账户没有了管理员权限

ML的编程技巧:

Flutter 小技巧之优化你使用的 BuildContext
随机推荐
[reading notes] deep learning Chapter 1: Introduction
重磅!《几何深度学习》新书发布,帝国理工/DeepMind等图ML大牛共同撰写,160页pdf阐述几何DL基础原理和统一框架
六轴传感器使用学习记录
Network design and planning of a company
房地产行业大洗牌
Intouch高级报警(报警筛选)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
Actual combat of MySQL database design project of online mall system
Scala foundation [set 01]
KCon 2022 亮点及议程大揭秘!
Beihang and other "deep learning event extraction" literature review paper, 27 page PDF describes the current trend
Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
授权无线通信标准
前夕 - 0day威胁情报
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Pymoo learning (6): termination conditions
Talk about 15 tips of SQL optimization
基于海思3559 高效率的 0延时 0拷贝 qt播放器方案
【刷题记录】21. 合并两个有序链表