I don't really think there was anything wrong with how were doing it but if we're experimenting with different ways to do this, perhaps pre-compute this limited set of data and use tables:
const byte
andMask[] =
{
0b11111111, //n=0 (not used)
0b11111100, //n=1
0b11110011, //n=2
0b11001111 //n=3
};
//X can equal 00, 01, 10 or 11
//n can equal 1, 2 or 3 (0 used as filler; not used)
const byte
orMask[4][4] =
{
{
0b00000000, //n=0, X=xx not used
0b00000000, //n=0, X=xx not used
0b00000000, //n=0, X=xx not used
0b00000000 //n=0, X=xx not used
},
{
0b00000000, //n=1, X=00
0b00000001, //n=1, X=01
0b00000010, //n=1, X=10
0b00000011 //n=1, X=11
},
{
0b00000000, //n=2, X=00
0b00000100, //n=2, X=01
0b00001000, //n=2, X=10
0b00001100 //n=2, X=11
},
{
0b00000000, //n=3, X=00
0b00010000, //n=3, X=01
0b00100000, //n=3, X=10
0b00110000 //n=3, X=11
}
};
int
n, X;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
n=0;
X=0;
}
void loop() {
byte
outputStatus;
// put your main code here, to run repeatedly:
for( n=1; n<4; n++ )
{
outputStatus = 0;
for(X=0; X<4; X++)
{
outputStatus = (outputStatus & andMask[n]) | (orMask[n][X]);
Serial.print("n= "); Serial.print(n); Serial.print(" ");
Serial.print("X= "); Serial.print(X); Serial.print(" ");
Serial.println(outputStatus,BIN);
delay(100);
}//for
}//for
}//loop