C++ Bitwise XOR Operator17 Mar 2025 | 8 min read
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++ |
C++ is a similar kind of programming language which merges the features of the C programming language and Simula67 (it was recognized as the first object Oriented language). C++ set up the concept of Classes and Objects. Are you looking for a good book to start with...
6 min read
The Declaration of functions inside and outside the main is the same as global and local variables. When we declare any function outside the main function, then it is globally defined, and it is in the global scope. When we define any function inside the main...
3 min read
This section will discuss the different ways to compare the given strings in the C++ programming language. The comparison of the string determines whether the first string is equal to another string or not. Example: HELLO and Hello are two different strings. There are different ways to...
5 min read
This section will discuss the Binary Operator Overloading in the C++ programming language. An operator which contains two operands to perform a mathematical operation is called the Binary Operator Overloading. It is a polymorphic compile technique where a single operator can perform various functionalities by taking...
5 min read
A hashing technique known as "cuckoo hashing" uses two or more hash tables to resolve collisions. It is predicated on the concept of multiple hash tables and two (or more) hash functions. An element is shifted to the available location in the other hash table...
5 min read
In this article, we will discuss the Babylonian square root algorithm in C++ with its history and examples. Introduction: The Babylonian square root algorithm, also known as Heron's method, is an iterative method to approximate the square root of a given number. It is based on the idea...
12 min read
In C++, OOP encapsulation refers to the grouping of data and related functions inside a single class. In other words, encapsulation is defined as the Binding (or wrapping) code and data together into a single unit. It restricts direct access to data and allows controlled modification...
9 min read
The object-oriented programming idea is supported by the general-purpose, middle-level, case-sensitive, platform agnostic computer language C++. Bjarne Stroustrup developed the C++ programming language in 1979 at Bell Laboratories. Since C++ is a platform-independent programming language, it may be used on a wide range of operating systems,...
4 min read
The std::unary_negate() is a wrapper function object. It returns the opposite of the unary condition it contains. A wrapper function is a procedure in a software library or computer program that is designed to invoke a second subroutine or a system call with little or no...
3 min read
In this example, we will discuss a C++ program to show runtime exceptions. But before discussing the implementation the runtime exceptions, we must know about the exceptions or exception handling in C++. Exceptions in C++: An exception in C++ is a problem that occurs while a program is...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India