Find the BCD difference of a number BCD difference

Given an integer n, convert it to BCD (binary-coded decimal)  by replacing each decimal digit with its 4-digit binary representation

 234 -&gt; 0 0 1 0 0 0 1 1 0 1 0 0


Then rotate the list of binary digits in order to find the largest and smallest numbers, representable by this list without other rearrangements.

max: 1 1 0 1 0 0 0 0 1 0 0 0  (the entire list rotated left 6 times)
min: 0 0 0 0 1 0 0 0 1 1 0 1 (the entire list rotated right 2 times)


Convert these numbers back to decimal, treating the list of bits as regular binary and subtract the smallest from the largest:

1 1 0 1 0 0 0 0 1 0 0 0 -&gt; 3336
0 0 0 0 1 0 0 0 1 1 0 1 -&gt; 141

3336 - 141 -&gt; 3195


The output is the difference of the largest and smallest numbers found.

Test cases:

234 -&gt; 3195
1234 -&gt; 52155
12 -&gt; 135
975831 -&gt; 14996295
4390742 -&gt; 235954919
9752348061 -&gt; 1002931578825
Wolfram Language (Mathematica), 89 88 bytes

Thanks to Jenny_mathy for saving 1 byte.

i=IntegerDigits;Max@#-Min@#&amp;[#~FromDigits~2&amp;/@NestList[RotateRight,Join@@i[i@#,2,4],#]]&amp;


Try it online!

This is terribly inefficient, because it generates n rotations of the BCD of n, which is way more than we need. We can make this is a bit more efficient by saving the result of the Join@@ in k and replacing the # at the end with Length@k. That lets us generate a scatterplot quite easily:



I'm really intrigued by the contrast of local structure and overall chaos.