Signup/Sign In
FEBRUARY 23, 2023

How to Get Logical XOR of two Variables in Python?

    How to Get Logical XOR of two Variables in Python?

    XOR (Exclusive Or) is a logical operation that performs a bitwise comparison between two binary values and returns a result based on the comparison. It is commonly used in computer programming, especially in cryptography and data security, to encrypt or decrypt information.

    In Python, XOR can be performed using the ^ operator. The ^ operator performs a bitwise XOR operation on the corresponding bits of the two binary values being compared. If the corresponding bits are different, the result is 1, otherwise, it is 0.

    How to Perform the Bitwise XOR in Python?

    In Python, you can perform bitwise XOR (exclusive OR) operation on two integers using the ^ operator.

    For example:

    a = 5
    b = 3
    c = a ^ b
    print(c)
    

    Output:

    6
    

    This means that the binary representation of 5 is 101, and the binary representation of 3 is 011. When you perform XOR on these two numbers, you get the binary representation of 6, which is 110.

    Perform XOR ^ Operator between 2 integers

    Yes, you're correct. The XOR operator ^ can be used between two integers to perform a bitwise exclusive OR operation in Python. The operation compares the corresponding bits of the two integers and returns a result where a bit is set if only one of the bits is set.

    Here's an example:

    a = 5
    b = 3
    result = a ^ b
    print(result)
    

    The output of the above code will be 6. This means that 5 in binary is 0b101 and 3 in binary is 0b011. When you perform a bitwise XOR between these two numbers, you get the binary representation of 6, which is 0b110.

    Performing XOR on two Boolean

    In boolean logic, the exclusive OR (XOR) operation is a binary operation that takes two input values (usually represented as true or false, or 1 and 0), and returns a true output value if exactly one of the inputs is true, and false otherwise. The truth table for XOR is as follows:

    Input 1 Input 2 Output
    0 0 0
    0 1 1
    1 0 1
    1 1 0

    So, if the first input is true (1) and the second input is false (0), the XOR operation will return true (1), and if both inputs are true (1), the XOR operation will return false (0).

    In most programming languages, the XOR operation can be performed using the "^" operator. Here's an example of how you could perform XOR on two booleans in Python:

    def xor(a, b):
        return (a and not b) or (not a and b)
    
    a = True
    b = False
    result = xor(a, b)
    print(result) # True
    

    And here's an equivalent implementation in Java:

    public static boolean xor(boolean a, boolean b) {
        return (a && !b) || (!a && b);
    }
    
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        boolean result = xor(a, b);
        System.out.println(result); // true
    }
    

    How to Swap two integers using XOR without a Temporary variable

    You can use the XOR bitwise operator to swap the values of two integers without using a temporary variable. Here's how it works:

    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    

    The first line uses the XOR operator to combine the bits of a and b into a single value.

    Combining the result of the previous XOR operation with b yields the original value of b in the second line. This is possible due to the fact that XORing a number with itself yields 0, which cancels out an in the previous result.

    Combining the current value of b with the previous result yields the original value of an in the third line, which uses the XOR operator once more. This is possible due to the fact that XORing a number with 0 yields the number itself.

    Consequently, a and b have been swapped without the use of a temporary variable.

    Perform XOR in Python using Operator Module

    The operator module in Python provides a set of functions that perform basic operations on two operands. To perform the XOR operation on two operands, you can use the xor function from the operator module. Here's an example:

    import operator
    
    a = True
    b = False
    result = operator.xor(a, b)
    print(result)

    In this example, a is set to True and b is set to False. When you perform the XOR operation on a and b using the xor function, the result is True.

    The xor function takes two operands as inputs and returns True if exactly one of the operands is True, and False otherwise.

    Conclusion

    In this article, you got to know about to get the logical xor of two variables in Python.

    Let us know some important points we learnt from this article:

    • The bitwise operation XOR stands for Exclusive OR.
    • The "^" symbol in Python is used to signify XOR.
    • The XOR technique allows us to swap two numbers without creating a temporary variable.
    • By importing the operator module, the xor() method may be used to perform XOR operations.

    Archishman Gupta is Fan of technology and all things Python. Informing readers with interesting writing about technological developments. Dedicated to helping more people understand advanced technological concepts.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS