为什么 SCHAR_MIN 在 C99 中定义为 -127? [英] Why is SCHAR_MIN defined as -127 in C99?

查看:13
本文介绍了为什么 SCHAR_MIN 在 C99 中定义为 -127?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99 的第 5.2.4.2.1 节将 SCHAR_MIN 定义为 -127,将 SCHAR_MAX 定义为 127.8 位有符号整数的范围不应该是 -128 到 +127?

我的编译器的 limits.h 将 SCHAR_MIN 定义为 (-1 << ((CHAR_BIT)-1)),它是 -128,因为 CHAR_BIT 是8.

SCHAR_MIN 被定义为 -127 而不是 -128 有什么原因吗?

解决方案

它实际上并没有将SCHAR_MIN定义为-127,它定义了有符号字符的最小范围到 -127..127.

这样做是因为它必须能够处理 其他 两种用于有符号数的编码方案,即反码和符号/大小.这两个都有一个正负零,偷走了你在二进制补码中找到的 -128.

ISO C (C99),6.2.6.2/2 节规定,实现必须为有符号整数数据类型选择以下三种不同表示形式之一:

  • 补码;
  • 反码;或
  • 符号/大小

两者的互补实现远远超过其他实现,但其他实现确实存在.

在所有这些表示中,正数是相同的,唯一的区别是负数.

要获得正数的负表示,您:

  • 将所有位取反,然后将二进制补码加一.
  • 将所有位反转为一个的补码.
  • 仅反转符号/幅度的符号位.

您可以在下表中看到 5 和 0:

<上一页>号码 |二进制补码|补码|符号/大小=======|======================|=====================|=====================5 |0000 0000 0000 0101 |0000 0000 0000 0101 |0000 0000 0000 0101-5 |1111 1111 1111 1011 |1111 1111 1111 1010 |1000 0000 0000 0101|||0 |0000 0000 0000 0000 |0000 0000 0000 0000 |0000 0000 0000 0000-0 |0000 0000 0000 0000 |1111 1111 1111 1111 |1000 0000 0000 0000(没有区别)(这两个都有不同的 +/-0)

§5.2.4.2.1 of C99 defines SCHAR_MIN as -127 and SCHAR_MAX as 127. Should not the range for an 8 bit signed integer be -128 to +127?

The limits.h for my compiler defines SCHAR_MIN as (-1 << ((CHAR_BIT)-1)), which is -128 given CHAR_BIT is 8.

Is there any reason why SCHAR_MIN was defined -127 and not -128 ?

解决方案

It doesn't actually define SCHAR_MIN as -127, it defines the minimum range of signed characters to -127..127.

It does this because it has to be able to handle the other two encoding schemes for signed numbers, those being ones' complement and sign/magnitude. Both of these have a positive and negative zero, stealing away the -128 you find in two's complement.

ISO C (C99), section 6.2.6.2/2, states that an implementation must choose one of these three different representations for signed integral data types:

  • two's complement;
  • ones' complement; or
  • sign/magnitude

The two's complement implementations far outweigh the others but the others do exist.

In all those representations, positive numbers are identical, the only difference being the negative numbers.

To get the negative representation for a positive number, you:

  • invert all bits then add one for two's complement.
  • invert all bits for ones' complement.
  • invert just the sign bit for sign/magnitude.

You can see this in the table below, for both 5 and 0:

number | two's complement    | ones' complement    | sign/magnitude
=======|=====================|=====================|====================
     5 | 0000 0000 0000 0101 | 0000 0000 0000 0101 | 0000 0000 0000 0101
    -5 | 1111 1111 1111 1011 | 1111 1111 1111 1010 | 1000 0000 0000 0101
       |                     |                     |
     0 | 0000 0000 0000 0000 | 0000 0000 0000 0000 | 0000 0000 0000 0000
    -0 | 0000 0000 0000 0000 | 1111 1111 1111 1111 | 1000 0000 0000 0000
           (no difference)        (both of these have distinct +/-0)

这篇关于为什么 SCHAR_MIN 在 C99 中定义为 -127?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆