如何计算整数溢出? [英] How do you calculate integers overflow?

查看:197
本文介绍了如何计算整数溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试根据给定的参数计算整数溢出. 例如,如果我有一个< == 200的整数,但是当我将它插入一个无符号的int时,它将是>200.对此的实际算术处理是什么?

I try to calculate, based on given parameters, integer overflow. for example, if I have an integer than is <= 200, but when I insert it to an unsigned int, it will be > 200. What is the actual arithmetic process for that?

推荐答案

对固定大小的整数进行的运算通常以2 m 为模,其中m是位数(如今通常为32或64)

Operations on fixed size integers are usually made modulo 2m, where m is the number of bits (nowadays usually 32 or 64).

这意味着从结果中添加或减去2 m 的倍数,以使其保持在该类型的范围内,无论它是无符号的(0,2 m -1)或带符号(-2 m-1 ,2 m-1 -1).

This means that a multiple of 2m is added or subtracted from the result to keep it in the range for the type, be it unsigned (0, 2m-1) or signed (-2m-1, 2m-1-1).

您可能对计算机整数的数学基础感兴趣

这篇关于如何计算整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆