 <p>Consider the following 'psuedo-c++'</p> <p>The code works as follows: </p> <ul> <li>take the highest, or lowest bit in the word by AND-ing with all zero's except the MSB or LSB, depending on the MSBFIRST flag</li> <li>write it to the output pin</li> <li>shift the command one step in the right direction</li> <li>pulse the clock pin</li> <li>repeat 8 times, for each bit in the command</li> </ul> <p>It is fairly trivial to expand this to an arbitrary number of bits upto 32 by adding a parameter for the number of repetitions</p> <pre><code>void shiftOut(GPIO dataPin, GPIO clockPin, bool MSBFIRST, uint8_t command) { for (int i = 0; i &lt; 8; i++) { bool output = false; if (MSBFIRST) { output = command &amp; 0b10000000; command = command &lt;&lt; 1; } else { output = command &amp; 0b00000001; command = command &gt;&gt; 1; } writePin(dataPin, output); writePin(clockPin, true); sleep(1) writePin(clockPin, false); sleep(1) } } </code></pre>