Python中的插入符(^)有什么作用? [英] What does the caret operator (^) in Python do?

查看:144
本文介绍了Python中的插入符(^)有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我在python中遇到了插入符号运算符,并对其进行了尝试,得到了以下输出:

I ran across the caret operator in python today and trying it out, I got the following output:

>>> 8^3
11
>>> 8^4
12
>>> 8^1
9
>>> 8^0
8
>>> 7^1
6
>>> 7^2
5
>>> 7^7
0
>>> 7^8
15
>>> 9^1
8
>>> 16^1
17
>>> 15^1
14
>>>

它似乎是基于8的,所以我猜是某种字节操作吗?除了对浮点数的奇怪表现之外,我似乎无法找到更多关于此搜索网站的信息,是否有人链接到该运算符的工作,或者您可以在此处进行解释?

It seems to be based on 8, so I'm guessing some sort of byte operation? I can't seem to find much about this searching sites other than it behaves oddly for floats, does anybody have a link to what this operator does or can you explain it here?

推荐答案

这是按位 XOR (或).

如果一个(并且只有一个)操作数(评估为)为true,则结果为true.

It results to true if one (and only one) of the operands (evaluates to) true.

演示:

>>> 0^0
0
>>> 1^1
0
>>> 1^0
1
>>> 0^1
1

要解释您自己的示例之一:

To explain one of your own examples:

>>> 8^3
11

这样想吧:


1000  # 8 (binary)
0011  # 3 (binary)
----  # APPLY XOR ('vertically')
1011  # result = 11 (binary)

这篇关于Python中的插入符(^)有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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