UINT_MIN 的可移植值是什么?

发布于 2024-09-14 14:56:54 字数 209 浏览 1 评论 0原文

limits.h 中,有 INT_MAXINT_MIN (以及 SHRT_* 和 LONG_* 等)的 #define,但只有 >UINT_MAX

我应该自己定义 UINT_MIN 吗? 0(正零)是可移植值吗?

In limits.h, there are #defines for INT_MAX and INT_MIN (and SHRT_* and LONG_* and so on), but only UINT_MAX.

Should I define UINT_MIN myself? Is 0 (positive zero) a portable value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦年海沫深 2024-09-21 14:56:54

它是一个无符号整数 - 根据定义,它的最小可能值为 0。如果您除了常识之外还需要一些理由,标准 说:

6.2.6.2 整数类型

  1. 对于除unsigned char之外的无符号整数类型,对象表示的位应分为两组:值位和填充位(后者不必是任何一个)。如果有 N 个值位,每个位应代表 1 到 2N−1 之间的 2 的不同幂,以便该对象type 应能够使用纯二进制表示来表示从 0 到 2N− 1 的值;这应称为值表示。任何填充位的值均未指定。

It's an unsigned integer - by definition its smallest possible value is 0. If you want some justification besides just common sense, the standard says:

6.2.6.2 Integer types

  1. 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.
池予 2024-09-21 14:56:54

您可以使用 std::numeric_limits::min()。

You could use std::numeric_limits<unsigned int>::min().

雨落星ぅ辰 2024-09-21 14:56:54

如果您想要“类型安全”,您可以使用 0U,因此,如果您在表达式中使用它,您将获得正确的提升为 unsigned

If you want to be "typesafe" you could use 0U, so if you use it in an expression you will have the correct promotions to unsigned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文