Bitshift Operators << >>

I am in the process of analyzing and understanding some code for a project I found on an Instructables for creating an 8x10 LED matrix. The project uses 74HC595 shift registers to multiplex the LEDs.

I need some help understanding the purpose of the Bitshift operators used in void display_patterns(). Why does the fourth parameter of the shiftOut() function need to shift bits to the left or right in order to display something on the Matrix?

Any help would would be much appreciated. :slight_smile:

http://www.instructables.com/id/810-LED-Matrix-with-4017/ instructables for the matrix project

-Skyler

int DataPin = 9;
int LatchPin = 11;
int ClockPin = 10;
int clock = 13;
int Reset = 12;
//x,y for the loop
int x;
int y;
// here the definion of all the letter(big ans small) and numbers

#define A     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010,B00000000}
#define B     {B00000000,B01111100,B01000010,B01000010,B01111100,B01000010,B01000010,B01000010,B01111100,B00000000}
#define C     {B00000000,B00111110,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B00111110,B00000000}
#define D     {B00000000,B01111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01111100,B00000000}
#define E     {B00000000,B01111110,B01000000,B01000000,B01111100,B01000000,B01000000,B01000000,B01111110,B00000000}
#define F     {B00000000,B01111110,B01000000,B01000000,B01111100,B01000000,B01000000,B01000000,B01000000,B00000000}
#define G     {B00000000,B00111100,B01000010,B01000010,B01000000,B01000111,B01000010,B01000010,B00111100,B00000000}
#define H     {B00000000,B01000010,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010,B01000010,B00000000}
#define I     {B00000000,B00111000,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00111000,B00000000}
#define J     {B00000000,B00011100,B00001000,B00001000,B00001000,B00001000,B01001000,B01001000,B00110000,B00000000}
#define K     {B00000000,B01000100,B01001000,B01010000,B01100000,B01010000,B01001000,B01000100,B01000010,B00000000}
#define L     {B00000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01111110,B00000000}
#define M     {B00000000,B01000100,B10101010,B10010010,B10010010,B10000010,B10000010,B10000010,B10000010,B00000000}
#define N     {B00000000,B01000010,B01100010,B01010010,B01001010,B01001010,B01001010,B01000110,B01000010,B00000000}
#define O     {B00000000,B00111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00111100,B00000000}
#define P     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111100,B01000000,B01000000,B01000000,B00000000}
#define Q     {B00000000,B00111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000110,B00111110,B00000001}
#define R     {B00000000,B00111100,B01000010,B01000010,B01000010,B01111100,B01000100,B01000010,B01000010,B00000000}
#define S     {B00000000,B00111100,B01000010,B01000000,B01000000,B00111100,B00000010,B01000010,B00111100,B00000000}
#define T     {B00000000,B11111110,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00010000,B00000000}
#define U     {B00000000,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00111100,B00000000}
#define V     {B00000000,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00100100,B00011000,B00000000}
#define W     {B00000000,B10000010,B10000010,B10000010,B10000010,B10010010,B10010010,B10101010,B01000100,B00000000}
#define X     {B00000000,B01000010,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B01000010,B00000000}
#define Y     {B00000000,B10000010,B01000100,B00101000,B00010000,B00010000,B00010000,B00010000,B00010000,B00000000}
#define Z     {B00000000,B01111110,B00000010,B00000100,B00001000,B00010000,B00100000,B01000000,B01111110,B00000000}

#define space {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000}

#define times {B00000000,B00000000,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B00000000,B00000000}

const int numPatterns = 17;//this is the number of patterns you want to display
byte patterns[numPatterns][10] = {space, M, A, D, E, space, B, Y, space, V, A, D, I, M, space}; // the patterns order

void setup() {
  pinMode(DataPin, OUTPUT);
  pinMode(ClockPin, OUTPUT);
  pinMode(LatchPin, OUTPUT);
  //simple stuff here
  pinMode(clock, OUTPUT);
  pinMode(Reset, OUTPUT);
  //reseting the 4017 IC, you have to do this
  digitalWrite(Reset, HIGH);
  delayMicroseconds(5);
  digitalWrite(Reset, LOW);
}

void display_pattern(int loops)//int loop acts like a delay, it take 8 mSecands to scan all of the rows so int loops = 15 is a good time for it
{
  for (x = 0; x < numPatterns - 1; x++) { // loop over the patterns
    for (int z = 0; z < 8; z++) { //scrolls one bite at a time
      for (int t = 0; t < loops; t++) { // the delay we get with loops
        for (y = 0; y < 10; y++) { // loops over the array of bytes
          byte temp = patterns[x][y];
          byte temp_2 = patterns[x + 1][y];
          digitalWrite(LatchPin, 0);
          shiftOut(DataPin, ClockPin, MSBFIRST, ((temp << z) + (temp_2 >> 7 - z))); //writes digital outputs, Z is for how much bites it need to scroll
          digitalWrite(LatchPin, 1); //data is only showed on the output after latch toggles from 0 to 1
          delayMicroseconds(800);
          digitalWrite(LatchPin, 0);
          shiftOut(DataPin, ClockPin, MSBFIRST, 0);
          digitalWrite(LatchPin, 1);
          digitalWrite(clock, HIGH);
          digitalWrite(clock, LOW);
        }
      }
    }
  }
}

void loop() {
  display_pattern(15);// calls for the display_pattern function and says that int loop = 15(if you do more loop the pattern whould scrrol slower).
}

The comment says it's for scrolling. A character display "scrolled" one position results from a bit pattern shifted one position. The added comlpexity (temp_2) is probably getting the next character in there too, if needed.

What westfw said. This:

 (temp << z) | (temp_2 >>> (8 - z))

scrolls temp left by z, and moves the leftmost z bits of temp_2 into the vacated z bits of tmp.

Let's say z is 3:

temp:    temp_2:
ABCDEFGH 76543210

temp << 3:
DEFGH---

temp2 >>> (8-3):
-----765

(temp << 3)|(temp2>>(8-3)):
DEFGH765

Now, as to why it's 7-z instead of 8-z: I don't know. The use of plus will do crazy things if temp_2 happens to have its top bit set, and will also do very bad things if temp_2 is a signed rather than an unsigned quantity (because the expression is using arithmetic shift rather than logical shift). Maybe this is the cause of whatever bug you are tracking down.

Does the use of '+' act the same as bitwise OR operator? and how does this simulate scrolling?

I really appreciate your help!!