14
Note: the first half of this challenge comes from Martin Ender's previous challenge, Visualize Bit Weaving.
The esoteric programming language evil has an interesting operation on byte values which it calls "weaving".
It is essentially a permutation of the eight bits of the byte (it doesn't matter which end we start counting from, as the pattern is symmetric):
- Bit 0 is moved to bit 2
- Bit 1 is moved to bit 0
- Bit 2 is moved to bit 4
- Bit 3 is moved to bit 1
- Bit 4 is moved to bit 6
- Bit 5 is moved to bit 3
- Bit 6 is moved to bit 7
- Bit 7 is moved to bit 5
For convenience, here are three other representations of the permutation. As a cycle:
(02467531)
As a mapping:
57361402 -> 76543210 -> 64725031
And as a list of pairs of the mapping:
[[0,2], [1,0], [2,4], [3,1], [4,6], [5,3], [6,7], [7,5]]
After 8
weavings, the byte is essentially reset.
For example, weaving the number 10011101
(which is 157
in base 10) will produce 01110110
(which is 118
in base 10).
Input
There are only 256
valid inputs, namely all the integers between 0
and 255
inclusive. That may be taken in any base, but it must be consistent and you must specify it if the base you choose is not base ten.
You may not zero-pad your inputs.
Output
You should output the result of the bit weaving, in any base, which must also be consistent and specified if not base ten.
You may zero-pad your outputs.
Related: Visualize Bit Weaving
5Fun fact: this is the challenge I wanted to post originally. Then I drew up the ASCII art to visualise the permutation, and then Sp3000 suggested that rendering that would make a better challenge. ;) – Martin Ender – 2016-06-29T13:13:30.160
2Can output base be different from input base? When you say "consistent" I understand that as "every possible input in the same base" – Luis Mendo – 2016-06-29T14:03:19.430
I think that the representation as a cycle would be more useful than the mapping representation. – mbomb007 – 2016-06-29T14:07:04.127
I have to say the ASCII art is definitely more fun. – Insane – 2016-06-29T19:32:22.340
2This could really use some more test cases. – James – 2016-06-29T23:22:04.257
Would taking input as a singleton list (i.e.
[1]
in Python instead of1
) be acceptable? – Mego – 2016-06-30T04:44:49.623@Mego I don't like trick questions but yes. – Leaky Nun – 2016-06-30T10:00:36.330
@LuisMendo Yes, and yes. – Leaky Nun – 2016-06-30T10:01:30.087
Where it says, "You may not zero-pad your inputs," that means
00000001
is invalid (should be1
), correct? – Joe – 2016-07-22T21:08:56.793@SirBidenXVII Correct. – Leaky Nun – 2016-07-23T00:29:09.460