常规 ^ 运算符
当给定两个布尔参数时, ^ 运算符执行异或,例如,
true ^ true == false
true ^ false == true
false ^ true == true
false ^ false == false
当给定两个数字参数时,它会执行某些操作,但我不知道是什么。起初我以为这是模除法,因为
(5 ^ 5) == 0
但是
(10 ^ 4) == 14
它不是模除法,是某种位移吗?
When given two boolean arguments, the ^ operator performs exclusive or, e.g.
true ^ true == false
true ^ false == true
false ^ true == true
false ^ false == false
When given two numeric arguments, it does something, but I've no idea what. At first I thought it was modular division because
(5 ^ 5) == 0
However
(10 ^ 4) == 14
So it's not modular division, is it some kind of bit-shifting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
^
的作用与 Java 和大多数其他语言中的作用相同:它是按位异或(简称:按位异或),
这意味着对于两个数字的二进制表示形式中的每一位,结果位输出中将是
bit_in_first_value ^ bit_in_second_value
。^
does the same thing as it does in Java and most other languages:It's a bitwise exclusive OR (short: bitwise XOR)
This means that for every bit in the binary representation of the two numbers the resulting bit in the output will be the
bit_in_first_value ^ bit_in_second_value
.