>>做什么和<<在Python中意味着什么? [英] What do >> and << mean in Python?

查看:253
本文介绍了>>做什么和<<在Python中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我可以做类似2 << 5来获得64和1000 >> 2来获得250的事情.

I notice that I can do things like 2 << 5 to get 64 and 1000 >> 2 to get 250.

我也可以在print中使用>>:

print >>obj, "Hello world"

这是怎么回事?

推荐答案

这些是按位移位运算符.

These are bitwise shift operators.

引用文档:

x << y

返回x,其位向左偏移y个位(右侧的新位为零).这与x2**y相同.

Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.

x >> y

返回x,其位向右移y个位.这与将x除以2**y相同.

Returns x with the bits shifted to the right by y places. This is the same as dividing x by 2**y.

这篇关于&gt;&gt;做什么和&lt;&lt;在Python中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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