-1>> 4和-1<< 4 [英] -1>>4 and -1<<4

查看:69
本文介绍了-1>> 4和-1<< 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,


我不是C级低级活动的专家,我需要知道

以下是如何工作的 -

有什么区别b / w方式-1<<< 4&-1>> 4工作??

Plz plz尽可能一点一点解释.. 。

提前付款..


再见

Sooraj

解决方案

so********@gmail.com ha scritto:

嗨朋友们,

我不是C级低级活动的专家,我需要知道
以下是如何工作的 -
有什么区别b / w的方式-1<<< 4&-1>> 4的工作原理??
Plz plz尽可能一点一点解释......
Thanx提前..




负数:<<和>>通过算术移位来实现。在

中,案例-1<< 4算术左移与逻辑左

移位相同。但在这种情况下:-1>> 4算术右移保留

符号位将其复制到你通常期望0从左边输入



至于结果,请记住,负数表示在

两个补码中(如果x是正数,-x是通过否定

x并添加1)它。


设'字节值:-1 = ~1 + 1 = 11111111.因此:


- 1<< 4 = 11110000 = -16


-1>> 4 = 11111111 = -1


再见。


所以******** @ gmail.com 写道:

我不是C级低级活动的专家,我需要知道
以下如何工作 -
有什么区别b / w -1<<< 4&-1;>> 4的工作方式??
Plz plz一点一点解释尽可能......
提前完成..



当x为负时,(x>> y)的结果是实现已定义。 />
你必须阅读你的编译器文档以了解会发生什么。

关于结果的假设是不可移植的。


For左移零填充空出的空格,任何符号位将被推迟到底。


该标准还定义了Y可以给出的提升了N位

表示X(基本上,它需要在[0,N]区间内)。


在文章< 11 ********************** @ i40g2000cwc.googlegroups .com>,

< so ** ******@gmail.com>写道:

我不是C级低级活动的专家,我需要知道
以下是如何工作的 -
有什么区别b / w的方式-1< ;< 4和-1>> 4工作??
Plz plz尽可能一点一点解释...




区别在于:


- 签署数量的左移不在C89

标准中讨论[特别是,一旦达到最小整数,则

如果再次移动,则不再讨论结果的标志]


- 签署数量的右移在C89标准中定义

作为依赖于实现的[特别是,实际上是关于将什么位值注入到新的
腾空的顶部位中。]


*无符号*数量的左右移动具有确切的语义

为所有情况定义,但你问的是移位-1(不是关于

v (一般来说)和-1从来都不是一个未签名的数量......

-

有些想法错了,只有一个非常聪明的人

可以相信它们。 - George Orwell


Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

Bye
Sooraj

解决方案

so********@gmail.com ha scritto:

Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..



On negative numbers: << and >> are implemented via arithmetic shifts. In
the case -1 << 4 the arithmetic left shift is the same as a logical left
shift. But in this case: -1 >> 4 the arithmetic right shift preserves
the sign bit copying it where you would normally expect 0s entering from
the left.

As for the results keep in mind that negative numbers are represented in
two''s complement (if x is a positive number, -x is obtained by negating
x and adding 1 to it).

Let''s suppose byte values: -1 = ~1 + 1 = 11111111. Hence:

-1 << 4 = 11110000 = -16

-1 >> 4 = 11111111 = -1

Bye.


so********@gmail.com wrote:

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..


The result for (x >> y) when x is negative is "implementation defined".
You will have to read your compiler docs to find out what happens.
Assumptions about the results would not be portable.

For left-shift zeros fill the vacated spaces and any sign-bit will be
pushed off the end.

The standard also defines what Y may be given the promoted N-bit
representation of X (basically, it needs to be in the interval [0,N]).


In article <11**********************@i40g2000cwc.googlegroups .com>,
<so********@gmail.com> wrote:

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...



The difference is:

- left-shifting of a signed quantity is not discussed in the C89
standard [in particular, once one has reached the minimum integer, then
if one shifts again sign of the result is not discussed]

- right-shifting of a signed quantity is defined in the C89 standard
as being implementation dependant [in particular, it is up to
the implementation as to what bit value is injected into the newly-
vacated top bit.]

Left and right shifting of *unsigned* quantities have exact semantics
defined for all cases, but you asked about shifting of -1 (not about
values in general) and -1 is never an unsigned quantity...
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


这篇关于-1&gt;&gt; 4和-1&lt;&lt; 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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