Method to iterate over the bits in a byte sent over serial
In all the examples below, value is the value of the byte

LSB -> MSB - so 00001111 would turn ON the first 4 devices and turn OFF the last 4

for(int i = 0, mask = 1; i &lt; 8; i++, mask = mask &lt;&lt; 1)
{
    if (value &amp; mask) 
    {
        // bit is on
    }
    else
    {
        // bit is off
    }
}


Alternatively, you can do the following, but value will be altered

for(int i = 0; i &lt; 8; i++, value = value &gt;&gt; 1)
{
    if (value &amp; 1) 
    {
        // bit is on
    }
    else
    {
        // bit is off
    }
}


Working the other way around in each byte:
MSB -> LSB - so 00001111 would turn OFF the first 4 devices and turn ON the last 4

for(int i = 0, mask = 128; i &lt; 8; i++, mask = mask &gt;&gt; 1)
{
    if (value &amp; mask) 
    {
        // bit "i" is on
    }
    else
    {
        // bit "i" is off
    }
}


Alternatively, you can do the following, but value will be altered

for(int i = 0; i &lt; 8; i++, value = value &lt;&lt; 1)
{
    if (value &amp; 128) 
    {
        // bit is on
    }
    else
    {
        // bit is off
    }
}