1

Considering the bitwise OR operation, wikipedia states,

$$x\;\mathrm{OR}\;y = \sum_{n=0}^b 2^n\left[\left[\left(\lfloor\frac{x}{2^n}\rfloor\mod2\right)+\left(\lfloor\frac{y}{2^n}\rfloor\mod2\right)\\+\left(\lfloor\frac{x}{2^n}\rfloor\mod2\right)\left(\lfloor\frac{y}{2^n}\rfloor\mod2\right)\mod 2\right]\mod2\right]$$

where $b$ is the number of bits in $x = \lfloor\log_2x\rfloor+1$ for all $x\neq0$

I can see that $\left(\lfloor\frac{x}{2^n}\rfloor\mod2\right)$ and $\left(\lfloor\frac{y}{2^n}\rfloor\mod2\right)$ are the $n^{th}$ bits of x and y, respectively, but I'm having a hard time putting the rest of the derivation together.

ivan
  • 3,237

2 Answers2

2

You can OR two bits, say $b_1$ and $b_2$ by $$\rm{OR}(b_1, b_2)=(b_1+b_2+b_1\cdot b_2) \mod 2$$ You can check to see that with this formulation, if either or both are $1$, we get $1$; while if both are $0$ we get $0$.

paw88789
  • 40,402
  • It seems then, that the wikipedia expression is misrepresenting the grouping, since it's showing $b_1 + b_2 + (b_1 * b_2 \mod2)$ – ivan Dec 15 '15 at 21:42
2

The expression $$\left[\left(\left\lfloor\frac{x}{2^n}\right\rfloor\mod2\right)+\left(\left\lfloor\frac{y}{2^n}\right\rfloor \mod2\right)\right]\ \mod2$$ will just compute $x\ XOR\ y$ instead of $x\ OR\ y$, because $1\ XOR\ 1=1_2+1_2=10_2\equiv 0\ (mod\ 2)$. To make $1\ OR\ 1=1$ instead of $0$, we should add $$\left[\left(\left\lfloor\frac{x}{2^n}\right\rfloor\mod2\right)\left(\left\lfloor\frac{y}{2^n}\right\rfloor\mod2\right)\right]\ \mod2$$ Which is $x\ AND\ y$. Now, $$1\ OR\ 1=[(1\ AND\ 1)+1+1]\mod 2=11_2\mod2=1$$

Hope it helps you understand.

Poypoyan
  • 972