English 中文(简体)
C 消极零标准(1个补充和签署规模)
原标题:C standard on negative zero (1 s complement and signed magnitude)

所有这些职能都给我的机器带来预期结果。 他们是否在其他平台上工作?

More specifically, if x has the bit representation 0xffffffff on 1 s complement machines or 0x80000000 on signed magnitude machines what does the standard says about the representation of (unsigned)x ?

此外,我认为,第2、第2a、第3、第4和第5卷(未签署)是多余的。 这是否正确?

Assume volume of (int) = 4 and CHAR_BIT = 8

int logicalrightshift_v1 (int x, int n) {

    return (unsigned)x >> n;
}

int logicalrightshift_v2 (int x, int n) {

    int msb = 0x4000000 << 1;
    return ((x & 0x7fffffff) >> n) | (x & msb ? (unsigned)0x80000000 >> n : 0);
}

int logicalrightshift_v2a (int x, int n) {

    return ((x & 0x7fffffff) >> n) | (x & (unsigned)0x80000000 ? (unsigned)0x80000000 >> n : 0);
}

int logicalrightshift_v3 (int x, int n) {

    return ((x & 0x7fffffff) >> n) | (x < 0 ? (unsigned)0x80000000 >> n : 0);
}

int logicalrightshift_v4 (int x, int n) {

    return ((x & 0x7fffffff) >> n) | (((unsigned)x & 0x80000000) >> n);
}

int logicalrightshift_v5 (int x, int n) {

    unsigned y;
    *(int *)&y = x;
    y >>= n;
    *(unsigned *)&x = y;
    return x;
}

int logicalrightshift_v6 (int x, int n) {

    unsigned y;
    memcpy (&y, &x, sizeof (x));
    y >>= n;
    memcpy (&x, &y, sizeof (x));
    return x;
}
最佳回答

If x has the bit representation 0xffffffff on 1 s complement machines or 0x80000000 on signed magnitude machines what does the standard says about the representation of (unsigned)x ?

改成<代码>unsign,以 Values而不是代表形式加以规定。 如果您将<代码>-1改为unsign,请您always<>em> 查询<代码>UINT_(如果您的<编码>unsign为32 bits,则始终请上429495<0>。 不管贵国执行时所签数目的多少,情况都是如此。

同样,如果您将<条码>-0改为<条码>。 然后,always,0。 <代码>-0在数字上等于0。

请注意,不要求补充或执行标志性激动,以支持消极零;如果它不这样做,那么获得这种代表就会导致该方案不明确的行为。

通过您的职能:

int logicalrightshift_v1(int x, int n)
{
    return (unsigned)x >> n;
}

这一功能对<代码>x的负值的影响,将取决于<编码>UINT_/2008/5/code>,并将进一步加以界定,条件是<代码>(未签名)x >> n不在<编码>int的范围之内。 举例来说,<代码>直径_v1(-1, 1)将回馈数值UINT_/2008/5/ 2,而不论机器对签名号码的用途是什么。

int logicalrightshift_v2(int x, int n)
{
    int msb = 0x4000000 << 1;
    return ((x & 0x7fffffff) >> n) | (x & msb ? (unsigned)0x80000000 >> n : 0);
}

这方面的几乎所有内容都可以确定执行。 假设你试图在<条码>中设定价值(<>mb>>/code>,在标线上设定一个数值,在计值栏中设定一个数值,则你不能通过使用轮值来这样做,你可以使用<条码>~INT_<>>,但允许在无法允许负零的信号振动机器上作出不明确的行为,并且允许对两个辅助机器实施规定的结果。

<代码>0x7fff和0x80000的类型将取决于各种类型的范围,这将影响这一表述中的其他价值观的推广。

int logicalrightshift_v2a(int x, int n)
{
    return ((x & 0x7fffffff) >> n) | (x & (unsigned)0x80000000 ? (unsigned)0x80000000 >> n : 0);
}

If you create an unsigned value that is not in the range of int (for example, given a 32bit int, values > 0x7fffffff) then the implicit conversion in the return statement produces an implementation-defined value. The same applies to v3 and v4.

int logicalrightshift_v5(int x, int n)
{
    unsigned y;
    *(int *)&y = x;
    y >>= n;
    *(unsigned *)&x = y;
    return x;
}

这一点仍在界定之中,因为没有说明在<代码>>上签字的比值是代表unsign<>/code>的比值或标的比值。 如果与婚礼相对应,它就可能成为一种陷阱,在这种情况下,行为没有定义。

int logicalrightshift_v6(int x, int n)
{
    unsigned y;
    memcpy (&y, &x, sizeof (x));
    y >>= n;
    memcpy (&x, &y, sizeof (x));
    return x;
}

适用于第5类的同样意见也适用于这种情况。

Also, I think the (unsigned) cast in v2, v2a, v3, v4 is redundant. Is this correct?

它取决于。 如该数值在<代码>int范围内,则<0x8000将具有<代码>int>。 如果该数值在<条码>范围内,则该数值在<条码>长期范围内;否则,<条码>在<条码>范围内;<条码>长期内(由于该数值在<条码>长期有效>范围内)。

如果你希望确保它有没有签名的类型,那么将固定的编号改为,到0x80000U


<>Summary:

  1. 编号大于INT_/2008/5/code> 至int,可得出一个执行界定的结果(或的确,允许发出一个执行规定的信号)。

  2. 将一个外围编号改为unsign,通过反复添加或减缩UINT_/2008/5/code>,这意味着该编号取决于数学 Value,而不是代表。

  3. <代码>int的表示不可行(正值int的表述为K.)。

  4. 通过使用双向操作商和试图使用由此产生的价值来达到负零是不能的。

如果你想要“逻辑转变”,那么你就应该利用任何地方的未签署类型。 签字的类型旨在处理“ > > > /em” 是什么事项而不是代表的算法。

问题回答

如果你将标准改为“字数”,则保证所有平台都不相同。

www.un.org/Depts/DGACM/index_spanish.htm 在诉5中,你违反了严格条款,这是不明确的行为。

In v2 - v4, You have signed right-second, which is implementation defined. (详情见评论意见)

www.un.org/Depts/DGACM/index_spanish.htm 在诉1中,你签署了未经签名的投稿,这是在数量超过范围时加以界定的执行。

EDIT:

<>strong>v6 >可根据以下假设实际开展工作:

  • int is either 2 s or 1 s complement.
  • unsigned and int are exactly the same size (in both bytes and bits, and are densely packed).
  • The endian of unsigned matches that of int.
  • The padding and bit-layout is the same: (See caf s comment for more details.)




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...