开发者

long ans = ((long) INT_MIN) * 2 - 1; causing a warning because of integer overflow

开发者 https://www.devze.com 2023-02-08 00:05 出处:网络
I have this in my code and it is causing the warning that follows: long ans = ((long) INT_MIN) * 2 - 1;

I have this in my code and it is causing the warning that follows:

long ans = ((long) INT_MIN) * 2 - 1;  

The warning that I get is:

warning: integer overflow detected: op "*"

I ha开发者_开发问答ve included limits.h so that I could use INT_MIN

#include <limits.h>


This means that the calculation will overflow the range of long. Signed overflow yields undefined behavior.

The only correlation between the range of values representable by long and the value INT_MIN is that INT_MIN is representable as a long. There is no guarantee that one less than twice INT_MIN is representable as a long.


On Windows x86, using the Visual C++ compiler, long is 4-bytes, same as int. Try long long

0

精彩评论

暂无评论...
验证码 换一张
取 消