C++ Bitwise XOR Operator
Truth Table of Exclusive OR (XOR) operatorLet there are two operands; the First one is A and the second one is B, the total combinations of input formed by these two operands will be 4. By using the following XOR truth table, we will determine the corresponding output. The result will be captured in C, here C = A ^ B. In this truth table, we are taking input in the form of bits, i.e., 0 and 1, and the output will also be generated in the form of bits, i.e., 0 and 1. Here, in the above XOR Truth table, we observe that when the values of operands A and B are different, i.e., ( 0, 1 ), ( 1, 0 ), the result that comes out will always be 1. And when the values of operands A and B are the same, i.e., ( 0, 0 ), ( 1, 1 ), the result that comes out will always be 0. Similarly, in this way, we can draw the truth table for Boolean values - Let there are two operands; the First one is A and the second one is B. The total combinations of input formed by these two operands will be 4. By using the following XOR truth table, we will determine the corresponding output. The result will be captured in C, here C = A ^ B. In this truth table, we are taking input in the form of Truth values, i.e., True ( T ) and False ( F ). The output will also be generated in the form of True values, i.e., T and F. Here, in the above XOR Truth table, we observe that, when the values of operands A and B are different, i.e., ( F, T ), ( T, F ), the result comes out will always be T. And when the values of operands A and B are same, i.e., ( F, F ), ( T, T ), the result comes out will always be F. From the tables above, we observe that T ( True ) is denoted by one and F ( False ) is denoted by 0. Steps to solve any given problem -
Execution of Bitwise Exclusive OR (XOR) operation in C++Let us understand in more detail about the execution of the XOR operation in C++ with the help of examples - Example 1: Find the exclusive OR of integer values; 10 and 14. Also, explain it and write the code of execution in C++.Solution: Let us consider two variables,' a ' and ' b ', to store the corresponding two operands given in the above question, i.e., 10 and 14. Here, a = 10 and b = 14. We will follow the below steps to find out the exclusive OR of the given two operands.
Explanation: a = 10 ( In Decimal form ) b = 14 ( In Decimal form ) Now, for an XOR b, we need to convert a and b in binary form - a = 1010 ( In Binary form ) b = 1110 ( In Binary form ) Now, applying XOR operation on a and b - a = 1010 b = 1110 --------------- a ^ b = 0100 ( In Binary form ) The result of a ^ b is 0100, which is in binary form. Now converting the result in decimal form, which is 4. 10 ^ 14 = 4 NOTE: By using the above XOR truth table, the output of corresponding bits are generated.We will now apply the bitwise XOR operation on 10 and 14 in C++ language and get the result, i.e., 4. C++ code for above example: Output Example 2: Find the exclusive OR of integer values; 3 and 15. Also, explain it and write the code of execution in C++.Solution: Let us consider two variables,' a ' and ' b ', to store the corresponding two operands given in the above question, i.e., 3 and 15. Here, a = 3 and b = 15. We will follow the below steps to find out the exclusive OR of the given two operands.
Explanation: a = 3 ( In Decimal form ) b = 15 ( In Decimal form ) Now, for an XOR b, we need to convert a and b in binary form - a = 0011 ( In Binary form ) b = 1111 ( In Binary form ) Now, applying XOR operation on a and b - a = 0011 b = 1111 --------------- a ^ b = 1100 ( In Binary form ) The result of a ^ b is 1100, which is in binary form. Now converting the result in decimal form, which is 12. 3 ^ 15 = 12 NOTE: By using the above XOR truth table, the output of corresponding bits are generated.We will now apply the bitwise XOR operation on 3 and 15 in C++ language and get the result, i.e., 12. C++ code for above example: Output Next TopicDifferent Ways to Compare Strings in C++ |