How many times was a 8 digit binary number circulary shifted?

32 Views Asked by At

Given an 8 digit binary number for example: $ 00000111 $, and the same number circularly shifted $k$ times, where $k < 8$, $ 11000001 $ can i calculate k without curcularly shifting the original number until it matches the second one?

2

There are 2 best solutions below

3
On BEST ANSWER

You can't calculate $k$ at all, even by circularly shifting. Suppose you shifted it $j$ times (for some $j\in\{0,1,2,3,4,5,6,7\}$ to get the original number. Then for any nonnegative integer $n,$ shifting the original number $8n-j$ times looks exactly the same as it would for any other such $n.$

The best you can do is calculate $k$ modulo $8.$

Edit: Even if you know that $k\in\{1,2,3,4,5,6,7\}$ you can't necessarily do any better. Consider the number $10101010$ to see why.

0
On

If you happen to start from 0101010101, you are out of luck because you cannot possible distinguish between $k=2$ and $k=0$ (and many more). But if we assume that the original pattern has not periodic at all, the following works without trial shifting:

Note that circular shifting $a$ is equivalent to computing $2a\bmod 255$ (we exclude the case of $a=255$ here). Hence we are given $2^ka\bmod 255$ and want to find $k$. By assumption, $a$ is not a multiple of $17$ (or else the two half-bytes would be equal). Hence we can compute $a^{-1}\bmod 17$ and hence $2^k\bmod 17$. From this compute $k$ via lookup: $$\begin{matrix} 2^k&1&2&4&8&9&13&15&16\\ k&0&1&2&3&7&6&5&4\end{matrix} $$