x &= 0b01010101; // mask out just the bits we want ( 0A0B0C0D )
x *= 0b0011; // this makes ( AABBCCDD )
x &= 0b01100110; // mask out again ( 0AB00CD0 )
x *= 0b0101; // this makes ( ABABCDCD0 )
x &= 0b01111000; // mask out again ( 0ABCD000 )
x >>= 3; // shift back ( 0000ABCD )
// to...
((((x&0b01010101)*0b0011)&0b01100110)*0b0101&0b01111000)>>3
// and pushed through a minifier...
(5*(3*(x&85)&102)&120)>>3
and then it all got a bit ridiculous:
g = Graphics.createArrayBuffer(8,8,2);
g.setColor(1);
g.drawLine(0,0,7,7);
g.setColor(2);
g.drawLine(7,0,0,7);
a = g.buffer;
// minified...
// ((((x&0b01010101)*0b0011)&0b01100110)*0b0101&0b01111000)>>3 -> 5*(3*(a[0]&85)&102)&120)>>3
// ((((x&0b10101010)*0b0011)&0b11001100)*0b0101&0b11110000)>>4 -> 5*(3*(a[0]&170)&204)&240)>>4
b = new Uint8Array([
(5*(3*(a[0]&85)&102)&120)>>3 | (5*(3*(a[1]&85)&102)&120)<<1, (5*(3*(a[0]&170)&204)&240)>>4 | (5*(3*(a[1]&170)&204)&240),
(5*(3*(a[2]&85)&102)&120)>>3 | (5*(3*(a[3]&85)&102)&120)<<1, (5*(3*(a[2]&170)&204)&240)>>4 | (5*(3*(a[3]&170)&204)&240),
(5*(3*(a[4]&85)&102)&120)>>3 | (5*(3*(a[5]&85)&102)&120)<<1, (5*(3*(a[4]&170)&204)&240)>>4 | (5*(3*(a[5]&170)&204)&240),
(5*(3*(a[6]&85)&102)&120)>>3 | (5*(3*(a[7]&85)&102)&120)<<1, (5*(3*(a[6]&170)&204)&240)>>4 | (5*(3*(a[7]&170)&204)&240),
(5*(3*(a[8]&85)&102)&120)>>3 | (5*(3*(a[9]&85)&102)&120)<<1, (5*(3*(a[8]&170)&204)&240)>>4 | (5*(3*(a[9]&170)&204)&240),
(5*(3*(a[10]&85)&102)&120)>>3 | (5*(3*(a[11]&85)&102)&120)<<1, (5*(3*(a[10]&170)&204)&240)>>4 | (5*(3*(a[11]&170)&204)&240),
(5*(3*(a[12]&85)&102)&120)>>3 | (5*(3*(a[13]&85)&102)&120)<<1, (5*(3*(a[12]&170)&204)&240)>>4 | (5*(3*(a[13]&170)&204)&240),
(5*(3*(a[14]&85)&102)&120)>>3 | (5*(3*(a[15]&85)&102)&120)<<1, (5*(3*(a[14]&170)&204)&240)>>4 | (5*(3*(a[15]&170)&204)&240)
]);
c = new Uint16Array(b.buffer);
for (i in c) print((c[i]|0x10000).toString(2).substr(1));
// outputs
1000000000000001
0100000000000010
0010000000000100
0001000000001000
0000100000010000
0000010000100000
0000001001000000
0000000110000000
And to swap the quadrants around, all you've got to do is swap the a[0,2,4,6] with a[9,11,13,15] in the insane block of code above.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
and then it all got a bit ridiculous:
And to swap the quadrants around, all you've got to do is swap the
a[0,2,4,6]
witha[9,11,13,15]
in the insane block of code above.