Replace digitalWrite with a selective bitwise operation
Personally I'd expand it out a little:

if (commandbits &amp; (1 &lt;&lt; i)) {
    PORTB |= (1&lt;&lt;3);
} else {
    PORTB &amp;= ~(1&lt;&lt;3);
}


It's far more readable, and there are less shifts to mess around with. Plus the AVR has bit set and bit clear operations which that will make use of, especially since it's now using literal values. You could actually replace those values with literals:

if (commandbits &amp; (1 &lt;&lt; i)) {
    PORTB |= 0x08;
} else {
    PORTB &amp;= 0xF7;
}