简体   繁体   English

16位数据类型范围内的混淆

[英]Confusion in 16-bit data-type range

In a 16 Bit C compiler we have 2 bytes to store an integer, and 1 byte for a character. 在16位C编译器中,我们有2个字节来存储整数,1个字节用于存储一个字符。 For unsigned integers the range is 0 to 65535. For signed integers the range is -32768 to 32767. For unsigned character, 0 to 255. According to the integer type, shouldn't the signed character range be like -128 to 127. But why -127 to 127? 对于无符号整数,范围是0到65535.对于有符号整数,范围是-32768到32767.对于无符号字符,0到255.根据整数类型,有符号字符范围不应该像-128到127.但是为什么-127到127? What about the remaining one bit? 剩下的一点怎么样?

I think you're mixing two things: 我认为你混合了两件事:

  1. What ranges the standard requires for signed char , int etc. 标准signed charint等要求的范围是多少。
  2. What ranges are implemented in most hardware these days. 目前,大多数硬件都实现了哪些范围。

These don't necessarily have to be the same as long as the range implemented is a superset of the range required by the standard. 只要实施的范围是标准要求的范围的超集,这些不一定必须相同。

According to the C standard , the implementation-defined values of SCHAR_MIN and SCHAR_MAX shall be equal or greater in magnitude (absolute value) to, and of the same sign as: 根据C标准SCHAR_MINSCHAR_MAX的实现定义值的大小(绝对值)应等于或大于 ,且符号相同:

SCHAR_MIN  -127
SCHAR_MAX  +127

ie only 255 values, not 256 . 只有255个值, 而不是256个

However, the limits defined by a compliant implementation can be 'greater' in magnitude than these. 但是,由兼容实现定义的限制可能比这些限制“更大”。 ie [-128,+127] is allowed by the standard too. 即标准也允许[-128,+127] And since most machines represent numbers in the 2's complement form , [-128,+127] is the range you will get to see most often. 由于大多数机器代表2的补码形式的数字[-128,+127]是您最常见到的范围。

Actually, even the minimum range of int defined by the C standard is symmetric about zero. 实际上, 即使 C标准定义的最小int范围也是零对称的 It is: 它是:

INT_MIN    -32767
INT_MAX    +32767

ie only 65535 values, not 65536 . 只有65535个值, 而不是65536个

But again, most machines use 2's complement representation, and this means that they offer the range [-32768,+32767] . 但同样,大多数机器使用2的补码表示,这意味着它们提供范围[-32768,+32767]

While in 2's complement form it is possible to represent 256 signed values in 8 bits (ie [-128,+127] ), there are other signed number representations where this is not possible. 2的补码形式中 ,可以用8位表示256个有符号值(即[-128,+127] ),还有其他有符号数表示,这是不可能的。

In the sign-magnitude representation , one bit is reserved for the sign, so: 符号幅度表示中 ,为符号保留一位,因此:

00000000
10000000

both mean the same thing, ie 0 (or rather, +0 and -0 ). 两者都意味着相同的东西,即0 (或更确切地说, +0-0 )。

This means, one value is wasted. 这意味着浪费了一个价值。 And thus sign-magnitude representation can only hold values from -127 ( 11111111 ) to +127 ( 01111111 ) in 8 bits. 因此,符号幅度表示只能保持从-127( 11111111 )到+127( 01111111 )的8位值。

In the one's complement representation (negate by doing bitwise NOT): 一个补码表示中 (通过按位NOT来否定):

00000000
11111111

both mean the same thing, ie 0 . 两者意味着相同的事情,即0

Again, only values from -127 ( 10000000 ) to +127 ( 01111111 ) can be represented in 8 bits. 同样,只有-127( 10000000 )到+127( 01111111 )的值才能用8位表示。

If the C standard required the range to be [-128,+127] , then this would essentially exclude machines using such representations from being able to efficiently run C programs. 如果C标准要求范围为[-128,+127] ,那么这将基本上排除使用这种表示的机器能够有效地运行C程序。 They would require an additional bit to represent this range, thus needing 9 bits to store signed characters instead of 8. The logical conclusion based on the above is: This is why the C standard requires [-127,+127] for signed characters. 它们需要一个额外的位来表示这个范围,因此需要9位来存储有符号的字符而不是8个。基于上述的逻辑结论是:这就是C标准对有符号字符需要[-127,+127]的原因。 ie to allow implementations the freedom to choose a form of integer representation that suits their needs and at the same time be able to adhere to the standard in an efficient way. 即允许实现自由选择适合其需要的整数表示形式,同时能够以有效的方式遵守标准。 The same logic applies to int as well. 同样的逻辑也适用于int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM