轮班操作 [英] Shift Operation

查看:80
本文介绍了轮班操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


您能否解释一下C标准是否支持使用某些运营商的逻辑权利

班次操作?


我知道某处我读到>>运算符。我想,这是在C,但是我认为我错了。没有谷歌帮我确定这个

在我的记忆中失效。 MSVC 6编译器给我错误。


谢谢&&此致,

Nishu

Hi All,

Could you please explain whether C standard supports logical right
shift operation using some operator?

I know somewhere I read about >>operator. I thought, it is in C but i
think i''m wrong about it. No where google helps me determining this
lapse in my memory. MSVC 6 compiler gives me error.

Thanks && Regards,
Nishu

推荐答案

Nishu说:
Nishu said:

大家好,


您能否解释一下C标准是否支持使用某些运营商的逻辑权利?b $ b转移操作?
Hi All,

Could you please explain whether C standard supports logical right
shift operation using some operator?



C'的右移运算符>>,其工作方式如下:


A> B将产生一个相当于A的结果,直接通过B

位位置。如果A是负数,则结果是实现定义的。


当然还有一个>> =运算符,这样你就可以做A>> = B相反

的A = A> B

C''s right shift operator, >>, works like this:

A >B will yield a result that is equivalent to A shifted right through B
bit positions. If A is negative, the result is implementation-defined.

There is also a >>= operator, of course, so that you can do A >>= B instead
of A = A >B


我知道某处我读到>>运算符。我想,这是在C
I know somewhere I read about >>operator. I thought, it is in C



不,''没有。不是。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

No, ''fraid not.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




Richard Heathfield写道:

Richard Heathfield wrote:

Nishu说:
Nishu said:

大家好,


您能否解释一下C标准是否支持使用某些运营商的逻辑权利?b $ b转移操作?
Hi All,

Could you please explain whether C standard supports logical right
shift operation using some operator?



C'的右移运营商>>,效果如下:


A> B将产生一个相当于A的结果,直接通过B

位位置。如果A为负数,则结果为实现定义。


C''s right shift operator, >>, works like this:

A >B will yield a result that is equivalent to A shifted right through B
bit positions. If A is negative, the result is implementation-defined.



如果我取一个带符号的整数,那么右移一个负int应该

给我另一个负int(带符号算术)或者一个正数整数。

这是C标准定义还是实现定义?


实际上我想知道是否

长A;

A = 0xFFFFFFFF;


A>> = 1;


A& = 0x80000000

if(A)

{

printf(" operator is arithmetic right shift");

}

其他

{

printf(运算符是逻辑右移);

}

谢谢。

Nishu

If i take a signed integer, so right shifting a negative int should
give me another negative int (signed arithmetic) or a positive integer.
Is this C standard defines or implementation defines?

Actually i want to know whether
long A;
A = 0xFFFFFFFF;

A >>= 1;

A &= 0x80000000

if(A)
{
printf("operator is arithmetic right shift");
}
else
{
printf(" operator is logical right shift");
}
Thanks.
Nishu


" Nishu" < na ********** @ gmail.comwrote:
"Nishu" <na**********@gmail.comwrote:

能否解释一下C标准是否支持逻辑正确

使用某些运算符进行移位操作?
Could you please explain whether C standard supports logical right
shift operation using some operator?



No.或者更确切地说,不可靠。


无条件整数的右移当然既不合逻辑也不

算术(或两者,如果你愿意的话);零从高位
结束转移,但没有符号位可以复制或不复制。


右移有符号整数是不同的正面和负面

有符号整数。

如果有符号整数的值为正或零,则零位从高端移入
就像无符号类型一样;这是不是因为它是一个逻辑转移,或者是否是b $ b这是一个带有零符号位被复制的算术转换而没有

的区别。毕竟,零位

等于任何其他零位。

但是,如果有符号整数具有负值,则得到

值是实现定义的。这意味着你的程序不允许在这个操作中崩溃[1],但是标准并不需要

任何特定的结果。它所要求的只是你的实现

定义了结果。这可能是一个合乎逻辑的转变,一个完全不同的东西(或者可能使得b
感觉到,例如,一个人的补充机器)。 br />

简而言之,如果你使用

正常的C>移位运算符,你的编译器_可能会使用逻辑右移,但这个假设是不可移植的:

并不能保证这会在您尝试的下一个平台上运行

on。

No. Or rather, not reliably.

Right shifting on unsigned integers is, of course, neither logical nor
arithmetical (or both, if you wish); zeros get shifted in from the high
end, but there is no sign bit to copy or not to copy.

Right shifting on signed integers is different for positive and negative
signed integers.
If the signed integer has a positive or zero value, zero bits are
shifted in from the high end just as for unsigned types; it makes no
difference whether this is because it is a logical shift, or whether
it''s an arithmetical shift with a zero sign bit being copied. A zero bit
is, after all, equal to any other zero bit.
If, however, the signed integer has a negative value, the resulting
value is implementation-defined. This means that your program is not
allowed to crash on this operation[1], but the Standard doesn''t require
any particular result. All it requires is that your implementation
defines what the result will be. This could be a logical shift, an
arithmetical shift, or something entirely different (which might makes
sense on, e.g., one''s-complement machines).

So in short, _your compiler_ might use a logical right shift if you use
the normal C >shifting operator, but that assumption is not portable:
there''s no guarantee that this will work on the next platform you try it
on.


我知道某处我读到的>>运算符。我想,这是在C,但是我认为我错了。
I know somewhere I read about >>operator. I thought, it is in C but i
think i''m wrong about it.



确实没有这样的事情。


Richard


[1 ] _left_-移位一个有符号整数,其结果将是
溢出;这是未定义的,因此可能会崩溃。

There is indeed no such thing.

Richard

[1] Unlike _left_-shifting a signed integer where the result would
overflow; that is undefined, and may therefore crash.


这篇关于轮班操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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