我正在寻找有关 Google Go 语言的信息。在“A Tour of Go”中,他们有这样的代码:
const (
Big = 1<<100
Small = Big>>99
)
但做什么<<
and >>
mean?
您可以在以下位置查看所有代码http://tour.golang.org/#14 http://tour.golang.org/#14
They are bitwise shift operators http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts. x << y
means x × 2y, while x >> y
means x × 2−y or, equivalently, x ÷ 2y. These operators are generally used to manipulate the binary representation of a value, where, just like with a power of 10 in decimal, multiplying or dividing by a power of two has the effect of “shifting” the digits left or right, respectively:
// Left shift:
13 * 2 == 26 // decimal
1101 * 10 == 11010 // binary (13 is 8 + 4 + 0 + 1)
// Right shift (brackets denote discarded portion):
13 / 2 == 6[.5] // decimal
1101 / 10 == 110[.1] // binary
由于您正在对整数进行操作,并且右移通常会产生小数值,因此有多种方法可以处理右移结果的舍入方式。在 Go 中,右移是逻辑移位 http://en.wikipedia.org/wiki/Logical_shift关于无符号值和算术移位 http://en.wikipedia.org/wiki/Arithmetic_shift关于有符号值。逻辑移位总是向零舍入,而算术移位总是向下舍入,即向−∞舍入。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)