Running out of memory when using many if statements

I guess this won't be memory friendly

It certainly won't

You could use the bits of a byte to hold each row of data.