INT_MAX (2147483647) 和INT_MIN (-2147483648)溢出

c语言中32位int型数据在运算的时候可能会出现溢出的情况,如:

-2147483648-1会得到什么结果?

-2147483648乘(-1)会得到什么结果?

2147483647+1会得到什么结果?

在编译器中运行一下得到的结果分别是:

-2147483648-1=2147483647;

-2147483648乘(-1)=2147483647

2147483647+1=-2147483648

下面解释出现这几个情况的原因:

①-2147483648的二进制表示为1000 0000 0000 0000 0000 0000 0000 0000,a-1=1000 0000 0000 0000 0000 0000 0000 0000+FFFFFFFF

很明显,这里会溢出,结果将变为7FFFFFFFF,用int表示就是2147483647。

②-2147483648*(-1),对-2147483648按位取反,得到7FFFFFFF就是2147483647。

③2147483647的二进制表示为7FFFFFFF,加1之后就变为了80000000,结果就是-2147483648。

原文地址:https://www.cnblogs.com/bigyang/p/8530304.html

时间: 2024-10-19 06:45:29

INT_MAX (2147483647) 和INT_MIN (-2147483648)溢出的相关文章

INT_MIN与溢出

隔了好久没更新了,因为我在学习PL和编译器/解释器的知识.挺好奇这方面的,因为没有学过相关的课程,所以学起来有点吃力,进展缓慢,所以导致没啥可写的. 今天看到这么一段话: 32位的int型的取值是2147483647 到 -2147483648,但是,在C/C++语言中,你不能直接使用 -2147483648 来代替最小负数,因为它不是一个数,而是一个表达式.表达式是:"对正数2147483648取负",所以,2147483648已经溢出了.这就是为什么INT_MIN总是定义成 (-I

String to Integer (atoi) leetcode

题目的意思是要将一个字符串转换成数字 这个题目的重点是要处理    各种各样的输入情况 在题目下面有一大段英文: Requirements for atoi: The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an opt

String to Integer (atoi)

1. Question 将字符串转换为整数,考虑各种输入情况: 空格处理:开头空格省略 有效数字:从第一个非空格字符开始的是+.-或数字,直到下一个非数字字符结束. 加号处理:开头加号省略 空串处理 溢出处理 无效数字处理 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do no

【leetcode】String to Integer (atoi)

String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended f

【LeetCode】8. String to Integer (atoi) 字符串转整数

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

【LeetCode】8 - String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

LeetCode 008 String to Integer (atoi)

[题目] Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to b

[LeetCode] String to Integer (atoi) 字符串

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【String to Integer (atoi) 】cpp

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be