- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 INT_MIN/-1
代码。我希望这会变成 INT_MAX+1(或翻转时为 0)。然而,我得到的实际结果是 INT_MIN。这是我的测试代码:
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
using namespace std;
int main()
{
int min=INT_MIN;
int res=min/-1;
printf("result: %i\n", res);
printf("max: %i min: %i\n", INT_MAX, INT_MIN);
return 0;
}
这个实现是特定的和/或未定义的行为吗?
最佳答案
Is this implementation specific and/or undefined behavior?
是的,signed 整数溢出是未定义的行为。根据 C++11 标准的第 5/4 段:
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [...]
注意,这同样不适用于无符号算术。如第 3.9.1/4 段和脚注 46 中所述:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo
2^n
wheren
is the number of bits in the value representation of that particular size of integer. [...]This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.
关于c++ - INT_MIN/-1 是 C++ 中定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176221/
我仍然没有找到为什么最低的带符号负数没有等效的带符号正数的原因?为了简单起见,我的意思是 3 位二进制数100 是-4?但我们不能有符号格式的正 4,因为我们不能。它溢出了。那么我们如何知道补码 10
我仍然没有找到为什么最低的有符号负数没有等效的有符号正数的原因?为简单起见,我的意思是 3 位二进制数100是-4?但是我们不能有正数 4 的签名格式,因为我们不能。它溢出了。那么我们如何知道二进制补
我正在解决一个关于二维数组的hackerrank问题,这是一个沙漏程序,我们必须找到每个沙漏的总和,然后打印最大的沙漏总和。代码如下 int hourglassSum(vector> arr) {
我希望 -2147483648 能够放入 4 个字节,因为它是使用 2 的补码表示的。 我错过了什么吗? INT MIN: -2147483648 INT MAX: 2147483647 sizeof
如果我有这样的事情怎么办: int a = 20; int min = INT_MIN; if(-a - min) //do something 如果正数大于 INT_MAX,则假设 INT_MIN。
这个问题在这里已经有了答案: Correct way to take absolute value of INT_MIN (5 个答案) Why the absolute value of the
我想在无符号中执行一些算术运算,需要取负整数的绝对值,比如 do_some_arithmetic_in_unsigned_mode(int some_signed_value) { unsign
我很困惑为什么结果被转换为 INT_MIN 而不是整数溢出? 有任何想法吗? #include int ft_atoi(const char *str) { int sign; in
谁能解释 GCC-7 编译器的以下行为? // main.cpp #include using namespace std; int main() { int a = -2147483648
gcc 为以下代码生成引发 SIGFPE 的 float 代码: #include int x = -1; int main() { return INT_MIN % x; } 但是,我在标
假设我有一个来自外部源的变量 i: int i = get_i(); 假设 i 是 INT_MIN 和二进制补码表示,-i 是未定义的吗? 最佳答案 这取决于平台。 C 支持三种负数表示法(参见 C9
嗯,我觉得我的问题比其他问题更复杂一些。我在尝试解决这个问题时意识到了这一点。 我尝试使用 int number = rand() % (INT_MAX - INT_MIN + 1) + INT_MI
我要解决的问题是: Implement pow(x, n), which calculates x raised to the power n (Leetcode problem 50) 我有以下代码
我需要你的一些智慧: 为什么不能这样做: std::vector min(INT_MIN, 3); “C++ 异常:内存位置 0x0000009AA63CF0A0 处的 std::length_err
这个问题在这里已经有了答案: for every int x: x+1 > x .... is this always true? (4 个答案) 关闭 9 年前。 for (i = 0; i <=
我怎样才能便携地找出 INT_MAX 和 abs(INT_MIN) 中的最小值? (这是 INT_MIN 的数学绝对值,而不是对 abs 函数的调用。) 它应该与大多数系统中的 INT_MAX 相同,
有符号整数在 x86 上通过二进制补码表示,其中符号位的值为 -(2^N)。这会产生 -2^N 和 2^N - 1 之间的典型可表示值范围(例如 -32768 到 32767)。 我很好奇,如果我将系
我正在尝试实现一个简单的程序来递归计算数字的幂。代码测试超过 INT_MAX 或 INT_MIN 的值,并且应该分配 power = -1。然而,即使经过一定数量的递归调用,当结果变量超过最大值时,它
我目前正在 www.udemy.com 上 C++ 类(class)在其中一节课中,老师正在使用 #include 演示一个整数可以拥有的最大整数个数。但是,当我去包括限制时,一切顺利,直到我尝试打印
signed long long value = -2147483648; cout << ((signed long long)value); 输出 2147483648(没有减号),为什么
我是一名优秀的程序员,十分优秀!