lshift& RSHIFT [英] lshift & rshift

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

问题描述

大家好,

每次骑一本关于语言C的文档或书籍我都看到了

运算符<<并且>存在。但每次他们说<<翻译

左边的数字(和> ...)但没有人说是否<< 1表示

每次a * 2,因为存在

整数的方式存在问题。所以我从不使用操作员,因为我担心在不同的架构上会出现奇怪的行为。我可以使用它们并且确保a<<<<<<<<< 1表示每台计算机上有* 2?是在语言C的ansi

标准中吗?

解决方案

即*** @ free.fr 说:


大家好,

每次骑着关于语言CI的文档或书籍都会看到

运算符<<并且>存在。但每次他们说<<翻译

数字



每一位移动到左边一个位置,最左边的位移到最后

进入比特桶(应该定期清空),最右边的

比特设置为0.


到左边(和> ...)



转移时注意已签名的类型。当转移负数时,危险特别严重 - 呃 -

危险。标志会怎样?

位? (答案:这取决于实施。)


但没有人说是否<< 1表示

每次a * 2,



不是每次都没有。例如,考虑一个CHAR_BIT为8的系统。


unsigned char ch = 1;


ch<< = 1; / * ch现在是2 * /

ch<< = 1; / * ch现在是4 * /

ch<< = 1; / * ch现在是8 * /

ch<< = 1; / * ch现在是16 * /

ch<< = 1; / * ch现在是32 * /

ch<< = 1; / * ch现在是64 * /

ch<< = 1; / * ch现在是128 * /

ch<< = 1; / * ch现在为0,即使2 * 128为256. * /


因为存在问题的方式

整数存储。所以我从不使用操作员,因为我担心在不同的架构上会出现奇怪的行为。我可以使用它们并且确保a<<<<<<<<< 1表示每台计算机上有* 2?



如果你坚持使用无符号类型,并且不要将任何1位推到最后,是的。

但为什么还要烦恼?为什么不乘以2,如果那是你想要做的?


是否在语言C的ansi标准中?



是的,但是 - 就像我说的那样 - 你需要小心。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


ie***@free.fr


大家好,

每次骑着关于CI语言的文档或书籍都会看到

运算符<<并且>存在。但每次他们说<<翻译

左边的数字(和> ...)但没有人说是否<< 1表示

每次a * 2,因为存在

整数的方式存在问题。所以我从不使用操作员,因为我担心在不同的架构上会出现奇怪的行为。我可以使用它们并且确保a<<<<<<<<< 1表示每台计算机上有* 2?是在ansi

语言C的标准?



使用位移运算符进行算术运算的问题之一就是

不再说出你的意思了。如果你想将某些东西乘以2,那么就这样说。


另一个问题是,对于签名类型,没有完全定义位移。

这部分是因为位移可以将值位移动到符号位,或者反映
反之亦然,部分原因是因为C允许对已签名的

整数进行不同的表示(2''补充,1'补码或符号量级。


在过去的糟糕时期,有些人习惯使用位移来执行简单的
乘法因为它运行得更快。这些天,当这种技巧可能时,编译器可以自己发现




对于算术运算,使用算术运算符。当你需要时节省位移

位移语义 - 例如,模拟移位寄存器。


-

Philip Potter pgp< atdoc.ic.ac.uk


在过去的糟糕时期,有些人习惯使用比特来执行简单的
< blockquote class =post_quotes>
乘法因为它运行得更快。这些天,当这种技巧可能时,编译器可以自己发现




对于算术运算,使用算术运算符。当你需要时节省位移

位移语义 - 例如,模拟移位寄存器。


-

Philip Potter pgp< atdoc.ic.ac.uk



因此,如果我想将int乘以2,我必须使用* 2(或a + a),

但不是<< ?确保它适用于每个架构(目前我不会忽略int的大小问题)。


Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit to the left (and >...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
strange behaviour on different architecture. Can I use them and be
sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?

解决方案

ie***@free.fr said:

Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit

Each bit is moved one place to the left, the leftmost bit falls off the end
into the bit bucket (which should be emptied regularly), and the rightmost
bit is set to 0.

to the left (and >...)

Beware signed types when shifting. The danger is particularly - er -
dangerous when right-shifting negative numbers. What happens to the sign
bit? (Answer: it depends on the implementation.)

but no one said if a << 1 mean
every time a * 2,

Not every time, no. For example, consider a system where CHAR_BIT is 8.

unsigned char ch = 1;

ch <<= 1; /* ch is now 2 */
ch <<= 1; /* ch is now 4 */
ch <<= 1; /* ch is now 8 */
ch <<= 1; /* ch is now 16 */
ch <<= 1; /* ch is now 32 */
ch <<= 1; /* ch is now 64 */
ch <<= 1; /* ch is now 128 */
ch <<= 1; /* ch is now 0, even though 2 * 128 is 256. */

because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
strange behaviour on different architecture. Can I use them and be
sure that a << 1 means a * 2 on every computer ?

If you stick to unsigned types, and don''t push any 1-bits off the end, yes.
But why bother? Why not just multiply by 2, if that''s what you want to do?

Is is in the ansi standard of language C ?

Yes, but - like I said - you do need to be careful.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


ie***@free.fr wrote:

Hello guys,
every time a rode a doc or a book about the language C I saw that
operators << and >exist. But each time they said that << translate
the digit to the left (and >...) but no one said if a << 1 mean
every time a * 2, because there is the problem with the way the
integer are stored. So I never use thoses operators because I fear
strange behaviour on different architecture. Can I use them and be
sure that a << 1 means a * 2 on every computer ? Is is in the ansi
standard of language C ?

One of the problems with using the bit-shift operators to do arithmetic is you
no longer say what you mean. If you want to multiply something by 2, say so.

Another problem is that bit-shifting is not entirely defined for signed types.
This is partly because a bit-shift could move a value bit into a sign bit, or
vice versa, and partly because C allows different representations of signed
integers (2''s complement, 1''s complement, or sign-magnitude).

In the bad old days, some people used to use bit-shifts to perform simple
multiplications because it would run quicker. These days, compilers can spot for
themselves when this sort of trick is possible and will do so.

For arithmetic, use arithmetic operators. Save bit-shifting for when you need
bit-shifting semantics - for example, simulating a shift register.

--
Philip Potter pgp <atdoc.ic.ac.uk


In the bad old days, some people used to use bit-shifts to perform simple

multiplications because it would run quicker. These days, compilers can spot for
themselves when this sort of trick is possible and will do so.

For arithmetic, use arithmetic operators. Save bit-shifting for when you need
bit-shifting semantics - for example, simulating a shift register.

--
Philip Potter pgp <atdoc.ic.ac.uk

So if I want to multiply an int by two I have to use a * 2 (or a + a),
but not << ? To be sure it works on every architecture (currently I
ignore the problem of the size of an int).


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

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