I was thinking 'bout some similar solution like Michael showed...
his way:
Code: Select all
t = (n&%1111010) | ((n&1)<<2) | ((n>>2)&1)
maybe it is a bit confusing...
1. you did not mention the higher bits, so we leave them out.
Michael preserved them for wich reason ever.
Code: Select all
t = ( n & %010 ) | ( (n & 1) << 2 ) | ( ( n >> 2 ) & 1)
2. Michael mixed up the order to shift and mask.
one bit he shift first, the other he mask first.
maybe it's getting clearer when we apply the same method on both.
additionally, we write the mask in binary.
Code: Select all
t = ( n & %010 ) | ( (n & %001) << 2 ) | ( ( n & %100 ) >> 2 )
3. last, we change the order of the operations to come to maximum readabilyty.
Code: Select all
t = ( ( n & %100 ) >> 2 ) | ( n & %010 ) | ( (n & %001) << 2 )
it is just the same, but clearer to see.
we access three bits
the left one is isolated then shifted to the right by 2
the middle one is isolated to be taken just the way it is
the right one is isolated and shifted to the left by 2
it's still cryptic enough, but maybe a bit less now.

oh... and have a nice day.