什么是<<在python中代表? [英] What does &lt;&lt; represent in python?

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

问题描述

Python 说

1 << 16 = 65536

<< 在 Python 中执行什么操作?

What operation does << performs in Python?

推荐答案

它是 Python 的左移运算符.顾名思义,左移操作将位向左移动.

It is the left shift operator for Python. A left shift operation, as the name says, move bits to the left.

假设您有 2 个二进制表示为 0010 的对象.所以 2<<2 表示将位向左移动两次:

Suppose you have 2 whose binary representation is 0010. So 2<<2 means to shift the bits twice to the left:

0010 -> 0100 -> 1000

0010 -> 0100 -> 1000

1000 是 8 的二进制表示.在数学上,左移与将数字乘以 2 的幂相同: a<<b == a*2^b ,但作为运算只通过移位完成,比乘法快得多.

1000 is the binary representation for 8. Mathematically, left shifting is the same as multiplying a number by a power of 2 : a<<b == a*2^b , but as the operation is done only by shifting, it is much faster than doing multiplications.

这篇关于什么是<<在python中代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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