Bitshifting Isn't Working right.

Ok so I'm trying to bit shift a byte of data over to implement scrolling on an led screen I made, however things were not working properly. Say I want to scroll across 0b10101010 on my screen, then my idea was to bit shift the data over in steps so as to make it look like it was scrolling. So:

testData
0b10101010 >> 1 translates to 0b01010101

2 0b00101010
3 0b00010101
4 0b00001010

You get the idea.

However when I tried implementing that idea into my program, it did not work AT ALL. So I wrote a quick 'For' loop that mimicked what my main code was doing and instead of writing it to the display, it would write it to the Serial Monitor.

for(int i=0; i<8; i++){
   Serial.print(testData)
   testData = testData >> i;
   delay(2000);
}

My results on the monitor were:
170 corresponds to 10101010
85 01010101
21 00010101
2 00000010
0 00000000

As you can see, it made the first bit shift correctly, however they seemed to jump far more than what I told it to do on the latter shifts. Any clue why this is happening and how I can fix it?

Where is the rest of your code, I can only guess that testData is even an integer type.

Your output does not look like it came from that code.

for(int i=0; i<8; i++){
   Serial.print(testData)
   testData = testData >> i;
   delay(2000);
}

You print the initial value and shift 0 bits in the first iteration. Your output should contain the starting value twice when the second iteration prints the data.

Honestly the rest of my code doesn't matter at this point. It would be way out of context for what I'm trying to solve here. All it is is the instructions for writing the information to some shift registers and a decade counter. The 'for' loop code that I posted is the only thing currently in the loop() function. What I'm trying to figure out is how to get this data to shift correctly, that way I can pass it in to my code and write it to the screen.

testData is of the type 'byte'...I thought that was implied by having it sitting over the 0b10101010, but I guess I was too vague.

I promise you 100% that that output came from my code. I could even do a video screen capture if you really wanted to see it in real time.

   testData = testData >> i;

Should be shifting testData by 1 not i

Or don't change testData and do this instead

  for (int i = 0; i < 8; i++)
  {
    Serial.println(testData >> i, BIN);
  }

What are you even trying to display ?

If you want to display some '1' characters and some '0' characters, then you need to display those. You can't display a single binary number as an actual binary number.

UKHelBob has it right, first time it shifts left no times next shifts it one, next shifts it two, that makes three overall, next shifts it three, that makes it three plus two plus one equals six and so on.

If you want to see all the bits in the output this does it, whereas the BIN parameter of Serial.print() suppresses the leading zeroes.

   for (int i = 0; i < 8; i++)
  {
    for (int bit = 7; bit > 0; bit--)
    {
      Serial.print(bitRead(testData >> i, bit));
    }
    Serial.println();
  }

Ahhhhh thanks Grumpy_Mike and UKHelibob! That makes sense. I redid the code a little bit so it resets the testData to it's original value after the bit shifting occurs. Now there is no more compounding shifts and it works like a treat! :slight_smile:

for (int i = 0; i < 8; i++)
  {
    testData = testData >> i;
    Serial.print(testData);
    Serial.println();
    testData = 0b10101010; //reset the data back to it's original value
    delay(1000);
  }

Now there is no more compounding shifts and it works like a treat!

Why change testData at all ?

 for (int i = 0; i < 8; i++)
  {
    Serial.println(testData >> i);
    delay(1000);
  }