C中的算术移位操作 [英] Arithmetic shift operation in C

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

问题描述

您好,

有人建议我操作员在C中执行算术移位吗?

可能是针对特定的编译器。


提前谢谢。


问候,

Shailendra

解决方案

< blockquote>

Le 15/06/2005 15:22,dans d8**********@ns2.fe.internet.bosch.com,?* Mehta

Shailendrakumar *? < SH ******************* @ de.bosch.com> aécrit*:

你好,
任何人都可以建议我操作员在C中执行算术移位吗?
可能是针对特定的编译器。

提前谢谢。

问候,
Shailendra



Ops<<和>>。

* n<< k = n * 2 ^ k

* n>> k = n / 2 ^ k

但要注意有符号/无符号的差异,

如在签名的情况下,传播一个高oerder位

(保持标志正确)。

只需写一个玩具程序即可。进行一些测试。


Mehta Shailendrakumar写道:

你好,
任何人都可以建议我操作员进行算术运算转移到C?
可能适用于特定的编译器。



在许多机器上,>>在signed int类型上运行将被称为

算术右移。


Mehta Shailendrakumar写道:

<你好,
有谁能建议我操作员在C中进行算术移位?
可能适用于一个特殊的编译器。




An你可以获得算术移位

当你多次或将整数除以2.


移动负值是实现定义的。

我知道的两种变化是

逻辑转变。和算术移位。

对于无符号类型,逻辑和算术移位是相同的。


-

pete


Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.

Thank you in advance.

Regards,
Shailendra

解决方案


Le 15/06/2005 15:22, dans d8**********@ns2.fe.internet.bosch.com, ?*Mehta
Shailendrakumar*? <sh*******************@de.bosch.com> a écrit*:

Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.

Thank you in advance.

Regards,
Shailendra


Ops are << and >>.
* n << k = n * 2^k
* n >> k = n / 2^k
But be careful with signed/unsigned differences,
As in the signed case, a high oerder bit is propagated
(to keep sign correct).
Just write a "toy program" to make some tests.


Mehta Shailendrakumar wrote:

Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.


On many machines, >> operating on a signed int type would be called an
arithmetic right shift.


Mehta Shailendrakumar wrote:


Hello,
Can anyone suggest me operator to perform arithmetic shift in C?
May it be for a perticular compiler.



An arithmetic shift is what you get
when you mutliply or divide an integer by 2.

Shifting negative values is implementation defined.
Two variations that I''m aware of are
"logical shift" and "arithmetic shift".
For unsigned types, logical and arithmetic shifts are the same.

--
pete


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

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