char 的整数值的范围是否取决于实现?

发布于 2024-07-08 05:15:26 字数 151 浏览 9 评论 0原文

我正在阅读《C++ 编程语言》,其中 Stroustrup 指出 char 的 int 值范围可以从 0 到 255 或 -127 到 127,具体取决于实现。 它是否正确? 看起来应该是从-128到127。如果不是,为什么在第二种实现可能性中它们只有255个可能的值,而不是256。

I'm reading The C++ Programming Language and in it Stroustrup states that the int value of a char can range from 0 to 255 or -127 to 127, depending on implementation. Is this correct? It seems like it should be from -128 to 127. If not, why are their only 255 possible values in the second implementation possibility, not 256.

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

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

发布评论

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

评论(5

倾听心声的旋律 2024-07-15 05:15:26

你陷入了二进制补码思维 - C++ 标准没有定义所使用的表示形式对于负数!

如果您的计算机(上帝禁止)使用补码来表示负数,那么您8 位字节的范围为 -127 到 + 127。 从好的方面来说,零有两种不同的可能表示...

但是,在现实世界中,你不太可能遇到一个互补的计算机

You're stuck in two's complement thinking - The C++ standard does not define the representation used for negative numbers!

If your computer (god forbid) uses ones's complement to represent negative numbers, you have a range of -127 to + 127 in an 8-bit byte. On the upside, you have two different possible representations for zero...

However, in the real world, you're unlikely to meet a one's complement computer.

泪冰清 2024-07-15 05:15:26

认为 unsigned char 的范围是从 0 到 255 的想法是错误的。这只是它的最小范围。 char 必须至少有 8 位,而有符号 char、无符号 char 和 char 本身确实可以有超过 8 位。 那么这意味着 unsigned char 可能会超过 255。虽然不可否认,我还没有一个超过 8 位的实现,但理论上这是可能的。 这是在 c89 标准(c++03 以此为基础)中指定的,记录文件limits.h(CHAR_BIT、UCHAR_MAX、CHAR_MAX)。

It's wrong to think that an unsigned char ranges from 0 to 255. that's only its minimal range. a char must have at least 8 bits, and signed char, unsigned char and char itself can have indeed more that just 8 bits. so then that means that unsigned char could go beyond 255. though admittedly, i haven't got an implementation where it had more than 8 bits, it theoretically is possible. that's specified in the c89 standard (on which c++03 bases), documenting file limits.h (CHAR_BIT, UCHAR_MAX, CHAR_MAX).

情场扛把子 2024-07-15 05:15:26

由于该标准没有提及任何有关 char 类型的信息,因此“char”可以是:

  1. 在某些编译器上“unsigned char”(0-255)(例如:TexasInstruments 的 ARM 处理器 - OMAP 系列编译器)

  2. 大多数编译器(gcc、MSVC ...)上的“signed char”(-128-127)

为了确保始终有一个明确定义的范围,您应该使用“signed char”或“unsigned char”。

Because the standard doesn't say anything about the char type, "char" can be:

  1. "unsigned char" (0-255) on some compilers (example: TexasInstruments compiler for their ARM processors - OMAP series)

  2. "signed char" (-128-127) on most compilers (gcc, MSVC ...)

To make sure you always have a well defined range you should use "signed char" or "unsigned char".

旧时模样 2024-07-15 05:15:26

C 和 C++ 中的字符类型

从阅读来看,它似乎可以可以是其中任何一个,具体取决于实施情况。

Character types in C and C++

From reading that it seems it can be any of those, depending on implementation.

浅沫记忆 2024-07-15 05:15:26

我目前的理解是存在三种可能性:

  • 如果值表示为无符号,则 char 的范围为 0 到 255。

  • 如果值表示为带符号的二进制补码,则 char 的范围为 -128 到 127。

  • 最后,如果值以带符号的补码表示,则 char 的范围为 -127 到 127。

最后一种可能性表明,与前两个实现的 256 个可能值相比,只有 255 个可能值,但是这个未能考虑补码表示中的负零。

My current understanding is that there are three possibilities:

  • If the values are represented as unsigned, a char will range from 0 to 255.

  • If the values are represented as signed in two's complement, a char will range from -128 to 127.

  • Finally, if the values are represented as signed in one's complement, a char will range from -127 to 127.

This final possibility suggests that there would only be 255 possible values in contrast to the 256 possible values for the first two implementations, but this fails to take into account the negative zero in one's complement representations.

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