Use of caret symbol( ^ ) in Ruby

前端 未结 1 1133
野性不改
野性不改 2020-12-09 15:58
1 ^ 1
# => 0

1 ^ 2
# => 3

5 ^ 6
# => 3

These are the results I am getting. Can, please, somebody explain how ^ works?

相关标签:
1条回答
  • 2020-12-09 16:34

    It's a bitwise XOR operator.

    For each bit in the binary representation of the operands, a bitwise XOR will get a 1 bit if one of the corresponding bits in the operands is 1, but not both, otherwise the XOR will get a 0 bit. Here's an example:

    5     = 101
    6     = 110
    5 ^ 6 = 011 = 3
    
    0 讨论(0)
提交回复
热议问题