C语言 有符号字符的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3898688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:39:00  来源:igfitidea点击:

Range of signed char

c

提问by Parikshita

Why the range of signed character is -128to 127but not -127to 128?

为什么有符号字符的范围是-128to127但不是-127to 128

回答by Chris Jester-Young

That is because of the way two's complementencoding works: 0 is treated as a "positive" number (signed bit off), so, therefore, the number of available positive values is reduced by one.

这是因为二进制补码编码的工作方式:0 被视为“正”数(有符号位关闭),因此,可用正值的数量减一。

In ones' complementencoding (which is not very common nowadays, but in the olden days, it was), there were separate values for +0 and -0, and so the range for an 8-bit quantity is -127 to +127.

一个人的补码编码中(这在现在并不常见,但在过去很常见),+0 和 -0 有单独的值,因此 8 位数量的范围是 -127 到 +127 .

回答by AnT

In 8-bit 2's complement encoding numbers -128and +128have the same representation: 10000000. So, the designer of the hardware is presented with an obvious dilemma: how to interpret bit-pattern 10000000. Formally, it will work either way. If they decide to interpret it as +128, the resultant range will be -127..+128. If they decide to interpret it as -128, the resultant range will be -128..+127.

在8位2的补数编码-128,并+128具有相同的表示:10000000。因此,硬件设计者面临一个明显的困境:如何解释位模式10000000。正式地,它会以任何一种方式工作。如果他们决定将其解释为+128,则结果范围将为-127..+128。如果他们决定将其解释为-128,则结果范围将为-128..+127

In actual real-life 2's complement representation the latter approach is chosen because it satisfies the following nice convention: all bit-patterns with 1in higher-order bit represent negative numbers.

在现实生活中 2 的补码表示中,选择后一种方法是因为它满足以下很好的约定:具有1高位的所有位模式表示负数。

It is worth noting though, that language specification does not require 2's-complement implementations to treat the 100...0bit pattern as a valid value in any signed integer type. E.g. implementations are allowed to restrict 8-bit signed charto -127..+127range and regard 10000000as an invalid bit combination (trap representation).

不过值得注意的是,语言规范不需要 2 的补码实现来将100...0位模式视为任何有符号整数类型中的有效值。例如,允许实现将 8 位限制signed char-127..+127范围并将其10000000视为无效的位组合(陷阱表示)。

回答by Yanick Rochon

I think an easy way to explain this for the common soul is :

我认为为共同的灵魂解释这一点的简单方法是:

A bit is a value 0or 1, or 2 possibilities

位是一个值01,或 2 种可能性

A 2-bit holds two combinations or 0and 1for four possible values : 00, 01, 10, and 11.

A 2位保持两个组合或01用于四个可能的值:000110,和11

A 3-bit holds three combinations for a total of eight possible values : 000to 111.

一个 3 位包含三种组合,总共有八个可能的值:000to 111

Thus n-bits holds n combinations for a total of 2^n possible values. Therefore, an 8-bit value is 2^8 = 256 possible values.

因此 n 位包含 n 个组合,总共有 2^n 个可能值。因此,一个 8 位值是 2^8 = 256 个可能的值。

For signed numbers, the most significant bit (the first one reading the value from left to right) is the sign bit; that leaves a possibility of 2^(n-1) possible values. For an 8-bit signed number, this is 2^7 = 128 possible values for each sign. But since the positive sign includes the zero (0 to 127 = 128 different values, and 128 + 128 = 2^8 = 256), the negative sign includes -1 to... -128 for 128 different values also. Where :

对于有符号数,最高有效位(从左到右读取值的第一个位)是符号位;这留下了 2^(n-1) 个可能值的可能性。对于 8 位有符号数,每个符号有 2^7 = 128 个可能值。但由于正号包括零(0 到 127 = 128 个不同的值,128 + 128 = 2^8 = 256),负号也包括 -1 到... -128 的 128 个不同值。在哪里 :

10000000 = -128
...
11111111 = -1
00000000 = 0
...
01111111 = 127

回答by user411313

#include <limits.h>
#include <stdio.h>
...

printf("range of signed character is %i ... %i", CHAR_MIN, CHAR_MAX );

回答by Omkar Palkar

If you look at ranges of chars and ints there seems to be one extra number on the negative side. This is because a negative number is always stored as 2's compliment of its binary. For example, let us see how -128 is stored. Firstly, binary of 128 is calculated (10000000), then its 1's compliment is obtained (01111111). A 1's compliment is obtained by changing all 0s to 1s and 1s to 0s. Finally, 2's compliment of this number, i.e. 10000000, gets stored. A 2's compliment is obtained by adding 1 to the 1's compliment. Thus, for -128, 10000000 gets stored. This is an 8-bit number and it can be easily accommodated in a char. As against this, +128 cannot be stored in a char because its binary 010000000 (left-most 0 is for positive sign) is a 9-bit number. However +127 can be stored as its binary 01111111 turns out to be a 8-bit number.

如果您查看字符和整数的范围,则负侧似乎有一个额外的数字。这是因为负数始终存储为其二进制的 2 的补码。例如,让我们看看 -128 是如何存储的。首先计算128的二进制(10000000),然后得到它的1的补码(01111111)。通过将所有 0 更改为 1 并将 1 更改为 0 来获得 1 的恭维。最后,存储这个数字的 2 的补码,即 10000000。2 的恭维是通过在 1 的恭维上加 1 得到的。因此,对于 -128,将存储 10000000。这是一个 8 位数字,它可以很容易地容纳在一个字符中。与此相反,+128 不能存储在 char 中,因为它的二进制 010000000(最左边的 0 表示正号)是一个 9 位数字。

回答by R.. GitHub STOP HELPING ICE

If you just consider twos complement as arithmetic modulo 256, then the cutoff between positive and negative is purely arbitrary. You could just as well have put it at 63/-192, 254/-1, 130/-125, or anywhere else. However, as a standard signed integer format, twos complement came by convention put put the cutoff at 127/-128. This cutoff has one big benefit: the high bit being set corresponds directly to the number being negative.

如果您仅将二进制补码视为算术模 256,那么正负之间的截止点纯粹是任意的。你也可以把它放在 63/-192、254/-1、130/-125 或其他任何地方。然而,作为标准的有符号整数格式,二进制补码按照惯例将截止值设为 127/-128。这种截止有一个很大的好处:设置的高位直接对应于负数。

As for the C language, it leaves the format of signed numbers up to the implementation, but only offers 3 choices of implementation, all of which use a "sign bit": sign/magnitude, ones complement, and twos complement.

至于C语言,它把有符号数的格式留给了实现,但只提供了3种实现选择,它们都使用了一个“符号位”:符号/大小、补码和二进制补码。