Help with logical bitshift/shift registers.

Im developing a kind of LED screen matrix which uses 16 bit shift registers... I don't have any experience with HW related programming as of 3 days ago :)

So... If I feed 16 bits like this: B00000000 B00000001 into the shift register, it lights the first LED green(1 is for green, 2 is for red, 3 is for blue)

SO.. In order to light the first 2 LEDs green I would need to feed in B00000000 B00001001

As far as I understand this is some kind of logical bitshift to the left, but I cannot understand WHAT it is...

B00000001 stands for 1 but B00001001 stands for 9 or 1 + 8

B01001001 stands for 73 which lights the first 3 leds green...

so there are portions of 3 1nes or zeros chained together and I cannot seem to find the logic how to shift :(

In other words - what is the shifting expression to get B0001001 out of B00000001


You can't just bit shift B00000001 to get B00001001. You need to combine bit shifting and bit anding to get the result you want.

But, there are bitRead and bitWrite functions that you could use that might be easier to understand. There are bitSet and bitClear functions, too.

Maybe somebody with a more math oriented mind can tell me how to combine those bits?

Is there even shifting involved?

There’s no shifting involved in getting the various bits set/cleared, you have a bit field of 16 bits (15 really, 3 x 5), you determne what colour is required at what “pixel”, set or clear the appropriate bits and shift the word out.

I see no need for maths as such.


One way to do it

int sr_word;

#define BLACK   0b00000000
#define GREEN   0b00000001
#define RED     0b00000010
#define BLUE    0b00000100
#define PURPLE  RED | BLUE
#define CYAN    BLUE | GREEN
#define WHITE   0b00000111
// etc etc

int masks [] = {

void SetPixel (byte pixel, int colour) {

  sr_word &= masks[pixel];
  sr_word |= colour << (pixel * 3);


void setup () {

void loop() {
  SetPixel (2, PURPLE);


You don’t need shifting, shifting does what it says it shifts to the left or right.
so if you have the value 1 and shift it to the left three times it becomes 8, shift it 4 times and it becomes 16. The value doubles for each time you shift it.

Setting bits is another matter see:-

Thank you for your answers very much! :)

I am comming close to a solution, will report this evening! :)