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

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

问题描述

C99定义的§5.2.4.2.1 SCHAR_MIN 为-127和 SCHAR_MAX 为127应该不是范围的8位有符号整数是-128到+127?

§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?

limits.h中我的编译器SCHAR_MIN定义为( - 1<<((CHAR_BIT)-1)),这是给-128是CHAR_BIT 8。

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

有没有为什么 SCHAR_MIN 定义-127和-128没有任何理由?

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

推荐答案

它实际上并不定义 SCHAR_MIN 为-127,它定义的最小范围<签署字符/ em>应用于-127..127。

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

它这样做是因为它必须能够处理的其他的有符号数两种编码方案,这是一补数(尽管许多参照本为一体的补,我倒是preFER信任克努特并就这一个ISO标准本身),并签署/幅度。

It does this because it has to be able to handle the other two encoding schemes for signed numbers, those being ones' complement (although many refer to this as one's complement, I'd prefer to trust Knuth and the ISO standard itself on this one) and sign/magnitude.

这两种有积极和消极的零,偷掉-128你补找到。

Both of these have a positive and negative zero, stealing away the -128 you find in two's complement.

ISO C(C99),截面 6.2.6.2/2 ,指出实现必须选择这三个不同的再presentations之一符号整数数据类型

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:


  • 补;

  • 一补数;或

  • 登入/幅度

2的补实现远远超过别人,但别人的的存在。

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

在所有这些重新presentations,正数是相同的,唯一的区别是负数。

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

要获得负重presentation为正数,您可以:

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


  • 反转所有位,然后添加一个二进制补码。

  • 反转所有位为一补数。

  • 转化在符号位为符号/幅度。

您可以在下面的表格中看到这一点,为5和0:

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天全站免登陆