检查C ++中的下溢/溢出?

编程入门 行业动态 更新时间:2024-10-11 07:35:04
本文介绍了检查C ++中的下溢/溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有一个通用的方法来检查给定数据类型(uint32,int等)的溢出或下溢吗?

我正在这样做:

uint32 a,b ,C; ... //初始化a,b,c if(b 当我打印一些迭代后,它显示一个大数字,如:4294963846。

code> uint32 a,b; //赋值 uint32 result = a + b; if(result< a){ //溢出}

您的具体检查将是:

if(a>(cb)){ / / underflow }

Is there a general way to check for an overflow or an underflow of a given data type (uint32, int etc.)?

I am doing something like this:

uint32 a,b,c; ... //initialize a,b,c if(b < c) { a -= (c - b) }

When I print a after some iterations, it displays a large number like: 4294963846.

解决方案

To check for over/underflow in arithmetic check the result compared to the original values.

uint32 a,b; //assign values uint32 result = a + b; if (result < a) { //Overflow }

For your specific the check would be:

if (a > (c-b)) { //Underflow }

更多推荐

检查C ++中的下溢/溢出?

本文发布于:2023-10-24 20:48:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!