The straightforward way is from a 4-bit word dcba
to first generate 0d0c0b0a
by shifting each bit in the right place:
t = (x & 0b0001) | ((x & 0b0010) << 1) | ((x & 0b0100) << 2) | ((x & 0b1000) << 3)
and then duplicate the bits:
return t | (t << 1)
A trickier alternative that needs fewer instructions is to first introduce a gap that leaves a
and c
in the right place:
t = (x | (x << 2)) & 0b00110011
and then shift b
and d
and introduce the duplication:
return ((t + (t | 0b1010101)) ^ 0b1010101) & 0b11111111
(the final &
is not needed if the data type is limited to 8 bits anyway).