Weird relation between bitwise ‘AND’ and ‘OR’ Operations.

binary operationsboolean-algebrafunctionsgraphing-functionslogic

I was trying to plot some boolean operations data using Matplotlib Python. I noticed some weird behavior of 'and'(&) and 'or'(|) operations on the same data pair.
Suppose:

A=50(0011 0010)
B=60(0011 1100)

A&B = 48(0011 0000)
A|B = 62(0011 1110)

Notice how both of them have the same distance(2) from the bigger and lower number. In other words- have the same distance(7) from the arithmetic average of A and B(55). Another example:

A=96(0110 0000)
B=82(0101 0010)

A&B = 64(0100 0000)
A|B = 114(0111 0010)

It is the same, A&B is 18 lower than B, and A|B is 18 higher than A. both are at the same distance from the mean of A and B(89) which is 25.

Mathematically:
$$
|(A.B – \mu(A,B))| = |(A+B – \mu(A,B))|
$$

It is almost true for all positive numbers(some weird problem arises if the lower bound gets under 0)
My First question is, why do they behave this way? Is this connected to De Morgan's theorem?

I tried to see if there is a relation between the distance that those operations have from the actual number(A and B) and the distance of A and B. I found this-

variable A = 10000, 10000, 10000, 10000,…..,10000
variable B = 10000, 10001, 10002, 10003,…..,15000

so on x-axis, I put B-A(0,1,2,3,4,….,5000).
On the y-axis, I put (A|B – B) (which is equal to (A – A&B))
The result was like this:
Matplotlib data of 5000 distances

Now my second question is, is it possible to get a function for this relation
Something like-
$$
f(y) = x^2 + sin(x)….
$$

If Anybody still reading, the plot for the distance of A&B and A|B from the arithmetic mean of A and B looks like this:
Distance from the mean of A and B plot

Same plot as before if A and B started from 20000, 20000. and plotted 10000 points instead of 5000.
10000 data points with A=20000, B starts from 20000
Thank you for your time:)

Best Answer

Consider numbers with 1 bit, then (with hopefully obvious terminology): $$A=0011$$ $$B=0101$$ $$A\&B=0001$$ $$A|B=0111$$ $$A\&B + A|B=0112$$ $$A+B = 0112$$ You should be able to see from there that the relationship you mention is true for any combination of single bits and so will also be true for any combination of bits.