---
title: "整数加减溢出"
author: "longxj"
date: "2009-11-11T15:21:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-11-11T15:21:00Z"
id: 570482
url: "https://longxj.iteye.com/blog/570482"
---

对于无符号整数，x，y：

x+y=x+y, x+y&lt;pow(2,w)

x+y-pow(2,w), x+y&gt;=pow(2,w)

对于有符号整数，x, y:

x+y=x+y,-pow(2,w-1)&lt;=x+y&lt;pow(2,w-1)

x+y-pow(2,w), pow(2,w-1)&lt;=x+y

x+y+pow(2,w), x+y&lt;-pow(2,w-1)

(w指整数的二进制表示的位数）

证明过程比较复杂，尤其是对于有符号整数的溢出情况，但是结果倒是挺完美的，如果只是用的话，很容易记住。