This is a C++ Program to Find the XOR of all Digits in a Given Binary Sequence.
The program takes a binary sequence and finds the XOR of all the digits. XOR gives a high value when there are odd number of 1s in the input.
1. The program takes a binary number.
2. Using a while loop, XOR of all digits in the number is calculated by counting the number of 1s.
3. If number of 1s is odd, then result is 1.
4. Else, it is 0.
5. The result is printed.
Here is the source code of C++ Program to Find the XOR of all Digits in a Given Binary Sequence. The program output is shown below.
using namespace std;
int main ()
int num, temp, res, x, i = 0;
cout << "Enter a binary number : ";
cin >> num;
temp = num;
while (temp != 0)
x = temp % 10;
if (x == 1)
temp = temp / 10;
if (i == 0)
res = 0;
if (i % 2 == 0)
res = 0;
res = 1;
cout << "\nExclusive OR of " << num << " is : " << res;
1. The user is asked to enter a binary number and it is stored in the variable num.
2. num is copied to a temporary variable temp. The variable ‘i’ is initialized as 0.
3. Using a while loop, temp is divided by 10 and the remainder is stored in the variable ‘x’.
4. If x is 1, the value of ‘i’ is incremented.
5. The loop terminates when temp is equal to 0.
6. If the value of i is 0 or even, then XOR of all the digits is 0 since there are 0 or even number of 1s.
7. Else, the result is 1.
8. The result is then printed.
Case 1 : Enter a binary number : 1110000011 Exclusive OR of 1110000011 is : 1 Case 2 : Enter a binary number : 11 Exclusive OR of 11 is : 0 Case 3 : Enter a binary number : 10101 Exclusive OR of 10101 is : 1
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.