C语言 有符号和无符号值是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13553707/
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 04:33:59  来源:igfitidea点击:

What does signed and unsigned values mean?

c

提问by Hello World

What does signedmean in C? I have this table to show:

signed在 C中是什么意思?我有这张表要显示:

enter image description here

在此处输入图片说明

This says signed char128to +127. 128is also a positive integer, so how can this be something like +128to +127? Or do 128and +127have different meanings? I am referring to the book Apress Beginning C.

signed char128+127. 128也是一个正整数,所以这怎么可能像+128to +127?或者do128+127有不同的含义?我指的是 Apress Beginning C一书。

回答by Pubby

A signed integer can represent negative numbers; unsigned cannot.

有符号整数可以表示负数;未签名不能。

Signed integers have undefined behavior if they overflow, while unsigned integers wrap around using modulo.

有符号整数在溢出时具有未定义的行为,而无符号整数使用模数环绕。

Note that that table is incorrect. First off, it's missing the -signs (such as -128 to +127). Second, the standard does not guarantee that those types mustfall within those ranges.

请注意,该表不正确。首先,它缺少-符号(例如 -128 到 +127)。其次,该标准不保证这些类型必须在这些范围内。

回答by jwaliszko

By default, numerical values in C are signed, which means they can be both negative and positive. Unsigned values on the other hand, don't allow negative numbers.

默认情况下,C 中的数值是有符号的,这意味着它们可以是负数也可以是正数。另一方面,无符号值不允许负数。

Because it's all just about memory, in the end all the numerical values are stored in binary. A 32 bit unsigned integer can contain values from all binary 0s to all binary 1s. When it comes to 32 bit signed integer, it means one of its bits (most significant) is a flag, which marks the value to be positive or negative. So, it's the interpretation issue, which tells that value is signed.

因为这一切都与内存有关,所以最终所有的数值都以二进制形式存储。一个 32 位无符号整数可以包含从所有二进制 0 到所有二进制 1 的值。当涉及到 32 位有符号整数时,这意味着它的一个位(最重要的)是一个标志,它将值标记为正数或负数。所以,这是解释问题,它告诉价值是有符号的。

Positive signed values are stored the same way as unsigned values, but negative numbers are stored using two's complementmethod.

正有符号值的存储方式与无符号值相同,但负数使用二进制补码方法存储。

If you want to write negative value in binary, first write positive number, next invert all the bits and last add 1. When a negative value in two's complementis added to a positive number of the same magnitude, the result will be 0.

如果你想用二进制写负值,先写正数,然后将所有位取反,最后加1。当二进制补码中的负值与相同大小的正数相加时,结果将为0。

In the example below lets deal with 8-bit numbers, because it'll be simple to inspect:

在下面的示例中,让我们处理 8 位数字,因为检查起来很简单:

positive 95: 01011111
negative 95: 10100000 + 1 = 10100001 [positive 161]
          0: 01011111 + 10100001 = 100000000
                                    ^
                                    |_______ as we're dealing with 8bit numbers,
                                             the 8 bits which means results in 0

回答by Lie Ryan

The table is missing the minuses. The range of signed char is -128 to +127; likewise for the other types on the table.

该表缺少缺点。有符号字符的范围是-128到+127;桌子上的其他类型也是如此。

回答by Julio

It was a typo in the book; signed char goes from -128 to 127.

这是书中的一个错字;有符号字符从 -128 到 127。

Signed integers are stored using the two's complementrepresentation, in which the first bit is used to indicate the sign.

有符号整数使用二进制补码表示存储,其中第一位用于表示符号。

In C, chars are just 8 bit integers. This means that they can go from -(2^7) to 2^7 - 1. That's because we use the 7 last bits for the number and the first bit for the sign. 0 means positive and 1 means negative (in two's complement representation).

在 C 中,字符只是 8 位整数。这意味着它们可以从 -(2^7) 到 2^7 - 1。那是因为我们使用最后 7 位作为数字,使用第一位作为符号。0 表示正,1 表示负(以二进制补码表示)。

  • The biggest positive 7 bit number is (01111111)b = 2^7 - 1 = 127.
  • The smallest negative 7 bit number is (11111111)b = -128
    (because 11111111 is the two's complement of 10000000 = 2^7 = 128).
  • 最大的 7 位正数是 (01111111)b = 2^7 - 1 = 127。
  • 最小的负 7 位数字是 (11111111)b = -128
    (因为 11111111 是 10000000 = 2^7 = 128 的二进制补码)。

Unsigned chars don't have signs so they can use all the 8 bits. Going from (00000000)b = 0 to (11111111)b = 255.

无符号字符没有符号,因此它们可以使用所有 8 位。从 (00000000)b = 0 到 (11111111)b = 255。

回答by Faizan

Signed numbers are those that have either + or - appended with them. E.g +2 and -6 are signed numbers. Signed Numbers can store both positive and negative numbers thats why they have bigger range. i.e -32768 to 32767

带符号的数字是附加有 + 或 - 的数字。例如 +2 和 -6 是有符号数。有符号数可以存储正数和负数,这就是它们具有更大范围的原因。即 -32768 到 32767

Unsigned numbers are simply numbers with no sign with them. they are always positive. and their range is from 0 to 65535.

无符号数字只是没有符号的数字。他们总是积极的。它们的范围是从 0 到 65535。

Hope it helps

希望能帮助到你

回答by KumarAnkit

Nobody mentioned this, but range of int in table is wrong: it is

没有人提到这一点,但表中 int 的范围是错误的:它是

-2^(31) to 2^(31)-1

i.e.,

IE,

-2,147,483,648 to 2,147,483,647

回答by rekordboy

Signed usually means the number has a + or - symbol in front of it. This means that unsigned int, unsigned shorts, etc cannot be negative.

有符号通常意味着数字前面有一个 + 或 - 符号。这意味着 unsigned int、unsigned shorts 等不能为负。

回答by 1-----1

A signed integer can have both negative and positive values. While a unsigned integer can only have positive values.

有符号整数可以同时具有负值和正值。而无符号整数只能有正值。

For signed integers using two's complement, which is most commonly used, the range is (depending on the bit width of the integer):

对于使用最常用的二进制补码的有符号整数,范围是(取决于整数的位宽):

char s -> range -128-127

char s -> range -128-127

Where a unsigned char have the range:

无符号字符的范围是:

unsigned char s -> range 0-255

unsigned char s -> range 0-255

回答by Fendrix

First, your table is wrong... negative numbers are missing. Refering to the type char.... you can represent at all 256 possibilities as char has one byte means 2^8. So now you have two alternatives to set ur range. either from -128 to +128 or 0 to 255. The first one is a signed charthe second a unsigned char. If you using integers be aware what kind of operation system u are using. 16 bit ,32 bit or 64 bit. Int (16 bit,32 bit,64 bit). char has always just 8 bit value.

首先,你的表是错误的......负数丢失。参考类型 char.... 你可以表示所有 256 种可能性,因为 char 有一个字节意味着 2^8。所以现在你有两种选择来设置你的范围。从 -128 到 +128 或 0 到 255。第一个是有符号字符,第二个是无符号字符。如果您使用整数,请注意您使用的是哪种操作系统。16 位、32 位或 64 位。整数(16 位、32 位、64 位)。char 始终只有 8 位值。