gpt4 book ai didi

c - 无需借助 `limits.h` 即可确定最大整数值的安全方法

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:43 26 4
gpt4 key购买 nike

这是在不使用 limits 库的情况下确定整数最大值的安全方法吗?

int max_int = (unsigned int) -1 >> 1;

最佳答案

我相信您提出的代码:

int max_int = (unsigned int) -1 >> 1;

不是确定带符号整数类型的最大值的安全方法(根据标准),但它绝对适用于大多数体系结构。我将尝试仔细地激发下面的陈述。

首先要注意的是:

6.2.5 Types

...

  • For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements. ...

  • The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the representation of the same value in each type is the same. A computation involving unsigned operands can never 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 type.

标准的这些行确保 (unsigned int) -1 被转换为 UINT_MAX。然后是:

6.5.7 Bitwise shift operators

...

  • The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2.

确保数字的值除以 2。现在到了棘手的地方。根据以下内容:

6.2.6.2 Integer types

  • For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N-1, so that objects of that type shall be capable of representing values from 0 to 2N - 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.
  • For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; there shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M <= N).

有符号整数类型包括:

  • M值BITS
  • X个填充位
  • 1 个符号位

而无符号整数类型由以下组成:

  • N值BITS
  • Y 填充位

由于相应类型的有符号和无符号整数必须具有相同的存储空间,因此我认为M+X+1 == N+Y

您的假设是,当仅移动一位时 M = N - 1,或者换句话说,int 中的填充位数与 unsigned int 相同。

现在,给定转换规则:

6.3.1.3 Signed and unsigned integers

  • When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
  • Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
  • Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

如果您对正在处理的架构的假设是正确的,您将命中第一颗子弹,否则您将命中第三颗,并且行为将实现定义

后一个选项不会“安全”,因为您不能确保仅基于 C 标准的程序具有独特的行为,并且您不能依赖实现来有一些功能,您可能需要正确处理这种情况。

关于c - 无需借助 `limits.h` 即可确定最大整数值的安全方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21073714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com