为什么-1>> 1 0xFFFFFFFF的>> 1产生不同的结果? [英] Why does -1 >> 1 and 0xFFFFFFFF >> 1 produce different results?

查看:775
本文介绍了为什么-1>> 1 0xFFFFFFFF的>> 1产生不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个测试来告诉被右移十六进制 FFFFFFFF 1

我知道,一个整数 1 内容 FFFFFFFF 十六进制,因为它是<$的补C $ C> 1 。右移 1 1 的结果 FFFFFFFF 和展示了PC进行算术右移。

但是,如果我只需键入 0xFFFFFFFF的&GT;&GT; 1 ,它导致了 7FFFFFFF 键,显示了PC进行逻辑右移代替。为什么会这样?请参阅code低于产生的结果:

 的#include&LT;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    输出(%×&GT;大于1 =%×\\ N,1,-1&GT;大于1);
    输出(%×&GT;大于1 =%×\\ N,为0xffffffff,为0xffffffff&GT;大于1);    返回EXIT_SUCCESS;
}

程序的输出是:

  FFFFFFFF&GT;&GT; 1 = FFFFFFFF
FFFFFFFF&GT;&GT; 1 = 7FFFFFFF


解决方案

这不是一个假设。什么键入的你觉得为0xffffffff 是?根据C标准, 6.4.4.1整型常量类型的十六进制的常数($ P $的前pression的的与 0X pceded)是第一个可容纳可适用重新presented值如下:

  INT
无符号整型
长整型
unsigned long int类型
得到long long int
无符号长长整型

psented为 INT

在你的平台上,为0xFFFFFFFF不能重新$ P $,因为 INT 是32位,只在 31位前preSS量符号int (该标准要求一位被保留的标志)。接下来的类型, unsigned int类型,因此使用。因此,没有符号位为present与换档操作,这是合乎逻辑的,从而,而不是算术扩展。

我是如何结束的 INT 是你的平台上的32位它可能不是很明显。事实上,我不能做这样的假设,如果不是对的第一个的线,算术右移的值-1 。这种转变,甩为%X 的结果,是 0xFFFFFFFF的。有 INT 被原生64位认为应该转储 0xFFFFFFFFFFFFFFFF 来代替。如果没有先验知识,可以假定 0xFFFFFFFF的的没有一个类型的结论,因为它很可能被重新presentable作为签署 INT 带宽64位(63 + 1)值 0x00000000FFFFFFFF 。由此产生的转变将产生你现在看到相同的输出,从而引入到上述假设的替代品。

I am trying to make a test to tell whether my PC performs arithmetic or logical right shift by right-shifting hexadecimal FFFFFFFF by 1.

I know that an integer -1 reads as FFFFFFFF in hexadecimal since it is the two's complement of 1. Right-shifting -1 by 1 results in FFFFFFFF and shows the PC performed arithmetic right shift.

But if I just type in 0xFFFFFFFF >> 1, it resulted in 7FFFFFFF and shows that the PC performed logical right shift instead. Why did that happen? See for the code below that produced the results:

#include    <stdlib.h>
#include    <stdio.h>

int main ( int argc, char *argv[] )
{
    printf ( "%x >> 1 = %x\n", -1, -1 >> 1 );
    printf ( "%x >> 1 = %x\n", 0xffffffff, 0xffffffff >> 1 );

    return EXIT_SUCCESS;
}

The program's output was:

ffffffff >> 1 = ffffffff
ffffffff >> 1 = 7fffffff

解决方案

It is not an assumption. What type do you think 0xffffffff is ? According to the C standard, 6.4.4.1 Integer constants, the type of the expression of a hexadecimal constant (preceded with 0x) is the first of the following which can applicably hold the represented value:

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int

On your platform, 0xFFFFFFFF cannot be represented as int because int is 32 bits and only 31 bits express quantity in signed int (the standard dictates one bit is reserved for sign). The next type, unsigned int, is therefore used. Therefore no sign bit is present to extended with the shift operation, which is thereby logical rather than arithmetic.

It may not be apparent how I concluded int was 32 bits on your platform. Indeed I could not make that assumption were it not for the first line, which arithmetic-right-shifts the value of -1. The result of that shift, dumped as %x, was 0xFFFFFFFF. Had int been native 64-bits that should dump 0xFFFFFFFFFFFFFFFF instead. Without that prior knowledge, no single type conclusion of 0xFFFFFFFF could be assumed since it may well be representable as a standard signed int of width 64-bits (63+1) with value 0x00000000FFFFFFFF. The resulting shift would produce the same output you see now, thereby introducing an alternative to that postulated above.

这篇关于为什么-1&GT;&GT; 1 0xFFFFFFFF的&GT;&GT; 1产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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