5

I found an observation by testing in C++.

Observation is ,

1 ) If two numbers where both numbers have odd number of set bits in it then its XOR will have even number of set bits in it.

2 ) If two numbers where both numbers have even number of set bits in it then its XOR will have even number of set bits in it.

1 ) If two numbers where one number has even number of set bits and another has odd number of set bits then its XOR will have odd number of set bits in it.

I could not prove it. I want to prove it. Please help me.

Code that i executed on my computer is

#include<bits/stdc++.h>
using namespace std;
int main(){
    vector<int> vec[4];
    for(int i=1;i<=100;i++){
       for(int j=i+1;j<=100;j++){ 
         int x=__builtin_popcount(i)%2;
         int y=__builtin_popcount(j)%2;
         int in=0;
         in|=(x<<1);
         in|=(y<<0);
         int v=__builtin_popcount(i^j)%2;
         vec[in].push_back(v);
      }
    }
      for(int i=0;i<4;i++){
         for(int j=0;j<vec[i].size();j++) cout<<vec[i][j] << " ";
         cout << endl;
      }
   return 0;
}

It gives me

100 zeros in first line 100 ones in second line 100 ones in third line 100 zeros in fourth line

If there is a doubt in understanding the code then please tell me in comments.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Kavar Rushikesh
  • 143
  • 1
  • 10

5 Answers5

6

Thanks all who tried to answer.

We can give proof like this,

Suppose N is number of set bits in first number and M is set bits in second number.

Then set bits in XOR of these two numbers is N+M - 2 (Δ) where is delta is total number of bit positions where both of numbers have set bit. Now this expression explains every thing.

even + odd - even = odd

odd + odd - even = even

even + even - even = even

Kavar Rushikesh
  • 143
  • 1
  • 10
5

This behavior mirrors an easy-to-prove arithmetical fact:

  • When you add two odd numbers, you get an even number,
  • When you add two even numbers, you get an even number,
  • When you add an odd number to an even number, you get an odd number.

With this fact in hand, consider the truth table of XOR, and note that for each of the four options in the table ({0, 0 => 0}, {0, 1 => 1}, {1, 0 => 1}, {1, 1, => 0}) the odd/even parity of the count of 1s remains invariant. In other words, if the input has an odd number of 1s, the output will have an odd number of 1s as well, and vice versa.

This observation explains why you observe the result: XORing two numbers with the counts of set bits of N and M will yield a number that has the same odd/even parity as N+M.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

xor just clears out common bits. It doesn't matter how many bits are set, just how many bits are common.

With all bits common, the result is zero. With no bits in common, the result is the sum of set bits.

No conclusions based on parity of inputs unless you also account for parity of common bits.

drawnonward
  • 53,459
  • 16
  • 107
  • 112
0

A possible proof is based in the observation that xor is a conmutative opperator, so (xor digits of x) xor (xor digits of y) = xor of digits of (x xor y)

0

I observed a connected relation while working on 2.65 in CSAPP and tried to express and prove it formally. It seems that my reasoning can also be applied to your question.

Let x and y denote arbitrary bit vectors.

Define function f as f(x) = (x has odd number of 1) ? 1 : 0.

To prove the observations, we need to express f(x ^ y) in terms of f(x) and f(y) and apply this identity for the cases listed.

The identity is (proof below)

(1) f(x ^ y) = f(x) ^ f(y)

For the cases:

1 ) If two numbers where both numbers have odd number of set bits in it then its XOR will have even number of set bits in it.

f(x)=1 and f(y)=1. Applying (1), f(x ^ y) = f(x) ^ f(y) = 1 ^ 1 = 0.

2 ) If two numbers where both numbers have even number of set bits in it then its XOR will have even number of set bits in it.

f(x)=0 and f(y)=0. Applying (1), f(x ^ y) = f(x) ^ f(y) = 0 ^ 0 = 0.

1 ) If two numbers where one number has even number of set bits and another has odd number of set bits then its XOR will have odd number of set bits in it.

f(x)=1 and f(y)=0. Applying (1), f(x ^ y) = f(x) ^ f(y) = 1 ^ 0 = 1.

f(x)=0 and f(y)=1. Applying (1), f(x ^ y) = f(x) ^ f(y) = 0 ^ 1 = 1.


Proof of:

(1) f(x ^ y) = f(x) ^ f(y)

Let n and i denote natural numbers

Let x . y denote the concatenation of two bit vectors.

First observe that

(2) f(x . y) = f(x) ^ f(y)

(it can be proved by case analysis on x and y having even or odd number of 1's)

Now prove (1) using math induction. For bit vectors x and y of length 1, (1) holds, by inspection. Assuming for some arbitrary n, n>=1 that (1) holds for any two bit vectors of length i with i<=n, consider two vectors x and y of length n+1 and this sequence of identities:

f(x) ^ f(y)
= f(x' . b_x) ^ f(y' . b_y)          [a]
= f(x') ^ f(b_x) ^ f(y') ^ f(b_y)    [b]
= f(x') ^ f(y') ^ f(b_x) ^ f(b_y)    [c]
= f(x' ^ y') ^ f(b_x . b_y)          [d]
= f((x' ^ y') . (b_x ^ b_y))         [e]
= f(x ^ y)                           [f]

[a] - split x of length n+1 into two bit vectors x' of length n and b_x of length 1, same for y (we can do that as n+1 >= 2)

[b] apply (2)

[c] xor is commutative and associative

[d] apply inductive hypothesis twice for sizes n and 1

[e] apply (2)

[f] by the definition of xor for bit vectors

Thus, by the principle of mathematical induction, (1) holds for any two vectors of size n for any natural n. Note that if two vectors are unequal in size, they can be brough to the same size by appending 0 to the lesser without effect on the outcomes of f.

Oleksii M
  • 1,458
  • 14
  • 22