当前位置:网站首页>[CSAPP Practice Problem 2.32] tsub_ OK (int x, int y) judge whether complement subtraction overflows
[CSAPP Practice Problem 2.32] tsub_ OK (int x, int y) judge whether complement subtraction overflows
2022-07-25 19:30: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.
First , Several concepts should be clarified :
- overflow : Refer to The result of computer calculation And The result of simple arithmetic operation Different .( The reason for this phenomenon is a certain type of digit (size) Not enough to accommodate the results , So it is called overflow .)
- Subtraction of complement : There is no subtraction unit inside the computer , So the subtraction operation is actually :x-y=x+(-y), Still apply addition , It's just a reversal of the minus sign .
- Complement negative : When x When it is the minimum , Its negative value is still itself , In other cases, the result is -x.
So said , When y It's not equal to INT_MIN when , The result should be tadd_ok(x, -y); And when y Exactly equal to INT_MIN when , because -y The result is still INT_MIN, So we need to discuss again .
When y==INT_MIN when :( For the convenience of discussion , hypothesis int Only those who have 4bit size .)
- if x It's a nonnegative number , for instance :x=[0010]->2,y=[1000]->-8, Note that the result of computer calculation is a, The result of pure arithmetic operation is b. be -y=[1000]->-8,a=x-y=x+(-y)=[0010]+[1000]=[1010]->-6,b=x-y=10.a!=b, Therefore, overflow occurs .
- if x It's a negative number , Take the same example :x=[1111]->-1,y=[1000]->-8, Note that the result of computer calculation is a, The result of pure arithmetic operation is b. be -y=[1000]->-8,a=x-y=x+(-y)=[1111]+[1000]=[0111]->7,b=x-y=7.a==b, Therefore, there is no overflow .
in other words , Correct tsub_ok function , It should be written like this :
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);
}
}Here is the official answer :
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.
There seems to be something wrong with the italics , The Chinese translation I checked is : When x When it's negative ,tsub_ok(x, TMin) Should be 1, When x When it is non negative ,tsub_ok(x, TMin) Should be 0. But the Chinese meaning of this sentence seems to be : When x When it's negative ,tsub_ok(x, TMin) Should be 0, When x When it is non negative ,tsub_ok(x, TMin) Should be 1.
I don't know whether there is a problem with this sentence or my English level .
边栏推荐
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
- Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
- leetcode刷题:动态规划07(不同的二叉搜索树)
- GBASE 8s UDR内存管理_02_mi_dalloc
- [hdlbits questions] Verilog language (3) modules: hierarchy section
- [wp]ctfshow-web入门信息搜集
- C# 合并集合
- Network data request for wechat applet development
- Monitor MySQL based on MySQL exporter
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(7)中期检查报告
猜你喜欢

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

Heavy! The new book "deep learning in geometry" was released, which was jointly written by imperial Institute of technology /deepmind and other figures ml Daniel. The 160 page PDF expounds the basic p

微信小程序10-微搭模板

网络数据包多层传输演示

二叉树可视化

安全基础6 ---漏洞复现

微信小程序开发之WXSS模板样式与WXS脚本语言

基于PHP的中非南南合作信息交流平台网站建设

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)

相机内参矩阵K和fov的相互转换
随机推荐
【阅读笔记】《深度学习》第一章:引言
Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
NPM semantic version control, solution console prop being mutated: "placement" error
[reading notes] deep learning Chapter 1: Introduction
How to be a self disciplined person?
[wp]ctfshow-web入门信息搜集
Hongmeng - Damiao computing Sketchpad - Introduction
Security foundation 6 - vulnerability recurrence
QIIME2得到PICRUSt2结果后如何分析
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
Openresty Lua resty mlcache multi-level cache
由一个蓝桥杯基础题报时助手而引出的常见误区
小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
Network design and planning of a company
JS learning notes 16: switching pictures small project practice
高并发下如何保证数据库和缓存双写一致性?
六轴传感器使用学习记录
房地产行业大洗牌
国内常见php的CMS建站系统情况分析
手机端触摸图片slider轮播插件photoswipe.js