为什么右移负数在C对最左边的位带来1? [英] Why does right shifting negative numbers in C bring 1 on the left-most bits?

查看:344
本文介绍了为什么右移负数在C对最左边的位带来1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这本书C完全参考手册由赫伯特Schildt说:(在签署负整数的情况下,右移将导致1被带进来,这样的符号位为preserved。 )

The book "C The Complete Reference" by Herbert Schildt says that "(In the case of a signed, negative integer, a right shift will cause a 1 to be brought in so that the sign bit is preserved.)"

什么是$ P $的pserving符号位点?

What's the point of preserving the sign bit?

此外,我认为这本书是指当负数重新$ P $的情况下使用符号位psented,而不是使用的。但仍即使在这种情况下,推理似乎没有任何意义。

Moreover, I think that the book is referring to the case when negative numbers are represented using a sign bit and not using two's complement. But still even in that case the reasoning doesn't seem to make any sense.

推荐答案

该Schildt本书被广泛认为是异常恶劣。

The Schildt book is widely acknowledged to be exceptionally poor.

事实上,C的的保证1将在当你右移负符号数被移位;的右移负值,结果是实现定义的。

In fact, C doesn't guarantee that a 1 will be shifted in when you right-shift a negative signed number; the result of right-shifting a negative value is implementation-defined.

然而,如果负数的右移位的的定义在1s至移位到最高位的位置,然后在2的补重新presentation它将表现为一个的算术移位的 - 用N右移位的结果将是相同的除以2 N ,向负无穷四舍五入

However, if right-shift of a negative number is defined to shift in 1s to the highest bit positions, then on a 2s complement representation it will behave as an arithmetic shift - the result of right-shifting by N will be the same as dividing by 2N, rounding toward negative infinity.

这篇关于为什么右移负数在C对最左边的位带来1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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