如何获取最小可能负整数的正整数值?

huangapple go评论72阅读模式
英文:

How to get positive integer value of least possible negative integer?

问题

The least possible value of the int type in C++ seems to be -2147483648.

C++中int类型的最小可能值似乎是-2147483648

Whenever my program encounters a negative integer, I want it to be converted into a positive (by multiplying it by -1):

每当我的程序遇到负整数时,我希望将其转换为正整数(通过乘以-1):

if (n < 0) n = (-1) * n;

如果n是负数,将其乘以-1以获得正数。

But, much to my chagrin, the constant compiler warning is:

但令我非常烦恼的是,编译器不断报警:

runtime error: signed integer overflow: -1 * -2147483648 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

运行时错误:有符号整数溢出:-1 * -2147483648 无法表示为类型'int'(solution.cpp)总结:UndefinedBehaviorSanitizer:未定义行为

Despite the compiler message being self explanatory, I can't find a way to overcome it.

尽管编译器的消息已经很明了,但我找不到克服它的方法。

I came across a question like https://stackoverflow.com/questions/4745617/, but it uses library functions like fabs(), abs(). I do not want to use built-in library functions.

我看到类似https://stackoverflow.com/questions/4745617/的问题,但它使用了库函数如fabs()abs()。我不想使用内置库函数。

Moreover, https://stackoverflow.com/questions/11243014/ shows the futility of abs() (albeit in C).

此外,https://stackoverflow.com/questions/11243014/展示了abs()的无效性(尽管是在C中)。

So, how to get the positive value of -2147483648 other than using this code?

那么,除了使用这段代码之外,如何获得-2147483648的正值?

if (n == -2147483648) n = 2147483648;

如果n等于-2147483648,则将其设置为2147483648。

英文:

The least possible value of the int type in C++ seems to be -2147483648.

Whenever my program encounters a negative integer, I want it to be converted into a positive (by multiplying it by -1):

> if (n &lt; 0) n = (-1) * n;

But, much to my chagrin, the constant compiler warning is:

> runtime error: signed integer overflow: -1 * -2147483648 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

Despite the compiler message being self explanatory, I can't find a way to overcome it.

I came across a question like https://stackoverflow.com/questions/4745617/, but it uses library functions like fabs(), abs(). I do not want to use built-in library functions.

Moreover, https://stackoverflow.com/questions/11243014/ shows the futility of abs() (albeit in C).

So, how to get the positive value of -2147483648 other than using this code?

> if (n == -2147483648) n = 2147483648;

答案1

得分: 2

Short Answer

你不能将2147483648存储在带符号的32位整数中,即使你这样做:

if(n==-2147483648) n=2147483648;

只需尝试cout结果,它将是-2147483648,因此存储的值是不正确的。

Long Answer

实际上,要理解存储的值不同的原因,你需要了解计算机如何存储有符号整数类型(具体是负值)。

存储负值的最简单方法是将第一个位作为符号指示器(0表示正数,1表示负数),但如果我们使用这种方式,那么会有0和-0,它们是相同的值,因此我们使用另一种方法,即二进制补码来存储负值,这样我们只有一个0和另一个负数(比正数多)。

让我们举个简单的例子:

假设我们需要在带符号的3位整数中存储-2(为了简单起见):

  1. 首先,我们需要将2从十进制转换为二进制,结果是010
  2. 其次,我们需要取010的二进制补码,将其从正数转换为负数,结果是110,这就是如何在带符号的3位整数中存储-2

现在让我们看看带符号的3位整数可以存储的值范围:

  • 从-4到3(负数比正数多,因为我们使用二进制补码存储负数)

现在让我们尝试将-4转换为4(不在带符号的3位整数范围内),看看存储的结果是什么

  1. 首先,将-4转换为二进制补码是100
  2. 然后反转二进制补码(将其转换为正数):not(100)+1 -> 011+1 -> 100,这与-4完全相同,因此存储的结果是-4,这意味着绝对值(-4)也是-4,因为4不在带符号的3位整数范围内

这就是你面临的问题。

Solution

如果你确实需要在变量中存储2147483648,有一些选项:

  1. 使用带符号的64位整数
  2. 使用两个变量,一个是无符号的32位整数,另一个是布尔型符号变量
英文:

Short Answer

You can't store 2147483648 in a signed 32-bit int even if you do:

if(n==-2147483648)     n=2147483648;

just try to cout the results, it will be -2147483648, so the value stored is incorrect

Long Answer

Actually, to understand why the value stored is different, you need to know how the computer stores signed int types (negative values in specific)

The easiest way to store negative values is to take the first bit as the sign indicator (0 is positive, and 1 is negative) ,but if we use this way, then there will be 0 and -0, which are the same value so we use another approach which is two's complement to store negative values and that gives us only one 0 and another number in the negative part (more than the positive)

Let's take a small example:

Let's say we need to store -2 in a signed 3-bit int (for simplicity):

  1. first, we need to convert 2 from decimal to binary which results in 010
  2. second, we need to take the two's complement of 010 to convert it from positive to negative, which results in 110 and that's how -2 stored in a signed 3-bit int

Now let's see the range of the values which a signed 3-bit int can store:

  • from -4 to 3 (negative is more than positive because we store the negative in two's complement)

Now let's try to convert -4 to 4 (which isn't in the range of a signed 3-bit int) to see what's the result stored

  1. first -4 in two's complement is 100
  2. then reverse the two's complement (to convert it to positive) by not(100)+1 -> 011+1 -> 100 which is exactly the same as -4 so the stored result is -4 and that means absolute(-4) is also -4 because 4 isn't in the range of a signed 3-bit int

and that's the same problem you have

Solution

You have some options if you really need to store 2147483648 in your variable

  1. Use a signed 64-bit int
  2. Use two variables one is an unsigned 32-bit int and the other is a boolean sign variable

huangapple
  • 本文由 发表于 2023年4月20日 09:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定