当前位置:网站首页>[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。
不清楚是是这一句英文有问题还是我英语水平的问题。
边栏推荐
- GBASE 8s UDR内存管理_01_mi_alloc
- 相机内参矩阵K和fov的相互转换
- Hash undirected graph visualization
- Flutter 小技巧之优化你使用的 BuildContext
- Improvement of wechat applet 26 playing music page ②
- 给容器添加3d效果的副标题
- Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
- 鸿蒙-大喵计算画板-简介
- Actual combat of MySQL database design project of online mall system
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
猜你喜欢

小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书

Scala基础【集合01】

微信小程序10-微搭模板

Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks

帝国CMS整站|手机号/QQ靓号商城源码|适配移动端

Wxss template style and WXS scripting language for wechat applet development

How to ensure the consistency of double write between database and cache?

Introduction to web security ICMP testing and defense

小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告

小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
随机推荐
Introduction to web security ICMP testing and defense
Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions
Imeta | sangerbox: interactive integrated clinical information analysis platform
C# 合并集合
网络数据包多层传输演示
JS learning notes 17: DOM query exercise
[applet development] detailed explanation of host environment
IP地址的概念
Hongmeng - Damiao computing Sketchpad - Introduction
哈希无向图可视化
相机内参矩阵K和fov的相互转换
485 current acquisition module dam-8041
小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
Kcon 2022 highlights and agenda revealed!
[hdlbits questions] Verilog language (3) modules: hierarchy section
Wxss template style and WXS scripting language for wechat applet development
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
微信小程序开发之WXSS模板样式与WXS脚本语言
[server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage