- $ll$ is a binary left shift
- $oplus$ is a bitwise XOR
- $0b1, 0b110, 0b10 ldots$ are used to denote raw bits
- $repr$ is a function associating to a number a given bit representation
- $mathbb{B}$ is the set of natural numbers to which $repr$ associates a two’s complement bit representation
- $mathbb{G}$ is the set of natural numbers to which $repr$ associates a Gray code bit representation
I was trying to prove that the binary representation of a power of $2$ in binary reflected Gray code was always two ones followed by zero or more zeroes, except for $2^0$. The demonstration is roughly as follows:
We know that the two’s representation of powers of $2$ can be expressed as follows: $$ forall n in mathbb{N} : repr(2_mathbb{B}^n) = 0b1 ll n $$ And the function $to_gray$ can be expressed as follows: $$ forall n in mathbb{B} : to_gray(n) = n oplus (n gg 1) $$ Therefore, we have the following equivalence (ignoring the special case when $n = 0$): $$begin{align*} forall n in mathbb{N} : repr(to_gray(2_mathbb{B}^n)) &= (0b1 ll n) oplus ((0b1 ll n) gg 1)\ &= (0b1 ll n) oplus (0b1 ll (n – 1))\ &= (0b10 ll (n – 1)) oplus (0b1 ll (n – 1))\ &= 0b11 ll (n – 1) end{align*}$$
However, the last line of the demonstration assumes that $ll$ distributes over $oplus$. Is this assumption true or does it only work for my specific use case?
Asked By : Morwenn
Answered By : Ilmari Karonen
As a consequence, for operations where $0 odot 0 ne 0$, such as NAND and NOR, the identity $(x odot y)_k = x_{k} odot y_k$ may not hold when $k$ is out of range. Thus, for such operations, the distributive properties given above may not hold for the lowest $n$ bits (for $ll n$) and the highest $n$ bits (for $gg n$, where $gg$ is a logical shift). However, arithmetic right shifts with sign extension (as well as right shifts on arbitrary-length numbers with implicit sign extension) do fully distribute over all bitwise logical operations.
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/58264