1.简单说一下python中^
, and
, or
的区别
-
按位异或:
^
(1)任何整数与0
按位异或都等于自身
(2)满足交换律,举个非常实用的例子:3^2^2=3^(2^2)=3^0=3
, 这样就可以很简单地记录出只出现了一次的数啦
(3)还可以判断bool
变量 True和False, 分别看成是1和0就行(4)相同的两个数的异或结果直接是
0
, 只有当两个数不同的时候才会转换为二进制进行异或运算 -
条件判断
and
(1)主要注意10000 and 3
的结果为3
,3 and 10000
的结果是10000
,这说明了是有顺序的
(2)更特殊的,0 and 10000
的结果为0
,而不是10000
,这是因为0
是作为了False -
条件判断
or
(1)主要注意10000 or 3
的结果为10000
,3 or 10000
的结果是3
,这说明了是有顺序的 -
[2020.10.02补]:
(1)<<
位运算是将整个二进制向左移动几位,末尾补零
(2)>>
位运算是将整个二进制向右移动几位