What is the usage of left and right shift bitwise operators >> and <<

huangapple go评论66阅读模式
英文:

What is the usage of left and right shift bitwise operators >> and <<

问题

我理解了整个问题,它是将位数向左移动

0000 0000 0000 0000 0000 0000 0000 0111 << 1
0000 0000 0000 0000 0000 0000 0000 1110

但是为什么要使用这些左移和右移运算符,而不是直接输入数字呢?在哪些情况下使用这些运算符会有好处呢?

我在Stackoverflow上看到很多关于运算符的回答,但没有人解释为什么要使用它们,而不是直接输入12345。所以,为什么要使用它们,它们相比于直接输入你想要的数字有什么好处呢?

我在浏览GitHub上的一个包时遇到了这段代码:

// ClientVersion 是 Client 实现的协议版本。
const ClientVersion = 1<<16 | 3<<8 | 0

这个数字的结果是:66304

既然这是一个常量,为什么不直接输入const ClientVersion = 66304,而要使用这些运算符呢?

英文:

I understand the whole, it shifts the bits over

0000 0000 0000 0000 0000 0000 0000 0111 &lt;&lt; 1
0000 0000 0000 0000 0000 0000 0000 1110

But why would you want to use these left and right shift operators instead of just typing out the number, where does using these operators actually have a benefit.

I see a lot of answers on Stackoverflow and what the operator accomplishes, but nobody ever says WHY they would use it over just typing out 12345 so like I said, why use them and what is their benefit over just typing the number out you're trying to get?

I came across this code while browsing a package on github:

// ClientVersion is the protocol version that Client implements.
const ClientVersion = 1&lt;&lt;16 | 3&lt;&lt;8 | 0

Which the number comes out to be: 66304

So if this is a constant why not just type const ClientVersion = 66304 why use the operators.

答案1

得分: 1

如果假设a是一个整数,那么a << xa乘以2^xa >> xa除以2^x,其中使用整数除法。

在你描述的情况下,我认为使用1<<16 | 3<<8 | 0代替66304没有真正的好处(除了展示你可以使用位运算符,但在我愚蠢的观点中,这是愚蠢的)。

但是有些情况下我认为它们是合理的(可以看看这个关于iota常量的问题)。

还有一些其他的例子(不仅与Go相关):

  • 检查第n位是否设置:x & (1<<n)
  • 设置第n位:x | (1<<n)
  • 对第n位进行其他操作。
英文:

If you assume a an integer, then a &lt;&lt; x multiplies a by 2^x and a &gt;&gt; x divides b by 2^x, where you use an integer division.

In the case that you described I see no real benefit of using 1&lt;&lt;16 | 3&lt;&lt;8 | 0 instead of 66304 (except of show-off that you can use bitwise operators, which in my stupid opinion is stupid).

But there are ways where I think that they are justifiable (take a look at this question about iota constants).

A couple of other examples (not only related to Go):

  • check if n-th bit is set x &amp; (1&lt;&lt;n)
  • set the n-th bit x | (1&lt;&lt;n)
  • many other manipulations with n-th bit.

答案2

得分: 0

当你需要小内存占用时,能够使用一个字节来存储256个不同的选项,而不是使用一个大的字符串数组或更多的字节,那么你将会使用这些技术。

嵌入式系统利用位运算来打开/关闭选项,任何需要在一定程度上自给自足并且不依赖于丰富的外部数据存储的系统都会从这种技术中受益。

英文:

When you require small memory footprints, being able to use a single byte to hold 256 different options instead of using a big string array or more bytes then you will use these.

Embedded systems make use of bit wise operations to turn on/off options, any system that needs to be somewhat self sustainable and not depend on a rich external datastore would benefit from this technique.

huangapple
  • 本文由 发表于 2016年1月7日 09:56:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/34646139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定