什么是>>用Go语言表示? [英] What does a >> mean in Go language?

查看:219
本文介绍了什么是>>用Go语言表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关Google Go语言的信息。在Go Tour of Go中,他们有以下代码:

I’m looking for information on Google’s Go language. In "A Tour of Go" they have this code:

const (
    Big = 1<<100
    Small = Big>>99
)

$ c>< 和>> 是指?

But what do << and >> mean?

您可以在 http://tour.golang.org/#14 上查看所有代码

You can see all of the code at http://tour.golang.org/#14

推荐答案

它们是按位移运算符 x<< y 意味着    2 ,而 x> > y 表示 x     2 − y 或等同于x ÷  ; 2 ý。这些运算符通常用于操作值的二进制表示,其中,就像十进制的幂乘以或除以2的幂,具有分别向左或向右移位数字的效果: / p>

They are bitwise shift operators. 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中,右移是无符号值的逻辑转换算术移位。逻辑移位总是向零移动,而算术移位总是向下移动,即朝向−∞。

Because you are operating on integers and a right shift typically results in fractional values, there are a couple of ways to handle how the result of a right shift is rounded. In Go, right shift is a logical shift on unsigned values and an arithmetic shift on signed values. Logical shift always rounds toward zero, while arithmetic shift always rounds down, that is, toward −∞.

这篇关于什么是&gt;&gt;用Go语言表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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