user70922: I have a 8x8 led matrix. I'm using LedControl.h library to control the matrix, which has 3 ways to light up the leds: setLed, setRow and setColumn. I've found that setColumn it's really slow, it's using setLed to print columns. With all my code, I can only print 3 times a second with setColumn. The function setRow is way faster, I'm guessing that the design of the matrix is allowing to print the rows directly. I can print more than 10 times a second with setRow, so I have to use it, since speed is crucial to my project. In my code, the data i have to print is stored in an array of 8 bytes. This is how it looks like when I use setColumn
for(int i = 7; i >= 0; i--)
    lc.setColumn(0, i, vector[i]);

i is the row to be printed, and vector[i] is a byte value, indicating which leds to be on or off. Column 0 is the left-most one, while column 7 is the right-most one, so i go from 7 to 0 to print from right to left, because that's what I have to do. My goal is to transform somehow to data in vector[i], to data that I can use it using setRow. So that's what I've done so far.
byte mask = 128, x;
for (int j = 0; j < 8; j++){
  x = 0;
  for(int p = 0; p < 8; p++){
    x = x | (valueVector[p] & mask);
    if (p != 7)
      x = x >> 1;
  }
  if (j != 7)
  mask = mask >> 1;
  lc.setRow(0, j, x);
}  

Now i print the rows from row 0 to row 7, from top to bottom. Since I'm printing from top to bottom, this means I have to take the last bit of every column (last bit of every element in vector), and put it in a byte variable. I was thinking to make a byte mask = 128 (b1000000, do a AND logic between to mask and the vector value in order to take the bit I'm looking for, and then making a OR to put the bit in the x byte. After every mask operation, I shift the x variable to the right, in order to make space for the next bit. After i'm done with a row, I shift the mask to the right, in order to take the second bit from the value in the vector. The result I get, is quite odd, I can't explain it, it's not good at all. What I'm i doing wrong with the mask and shifts? I hope I explained well what I'm trying to do. If necessary, I can provide more information. Thanks.

user10285: You use a mask 10000000. With each iteration of the outer loop the 1 in the mask is shifted to the right. 01000000 -> 00100000 -> 00010000. Let's say we are in the 2nd iteration the mask is 01000000. x = x | (valueVector[p] & mask); takes the 2nd bit (counted from left) and sets it at the 2nd position in the x value. But this position contains the bit from the last iteration (as you shifted it to this position).
Possible solution: (I have no compiler at hand. So hopefully it is correct. ;-)
byte x;

for ( int j=0; j<8; j++ )
{
   x = 0;

   for ( int p=0; p<8; p++ )
     x = x | ((valueVector[p] & (1 << p)) >> p);

   lc.setRow(0, j, x);
}  


70922: <p>I have a 8x8 led matrix. I'm using LedControl.h library to control the matrix, which has 3 ways to light up the leds: setLed, setRow and setColumn. I've found that setColumn it's really slow, it's using setLed to print columns. With all my code, I can only print 3 times a second with setColumn. The function setRow is way faster, I'm guessing that the design of the matrix is allowing to print the rows directly. I can print more than 10 times a second with setRow, so I have to use it, since speed is crucial to my project. In my code, the data i have to print is stored in an array of 8 bytes. This is how it looks like when I use setColumn</p>
<pre><code>for(int i = 7; i &gt;= 0; i--)
    lc.setColumn(0, i, vector[i]);
</code></pre>
<p>i is the row to be printed, and vector[i] is a byte value, indicating which leds to be on or off. Column 0 is the left-most one, while column 7 is the right-most one, so i go from 7 to 0 to print from right to left, because that's what I have to do. My goal is to transform somehow to data in vector[i], to data that I can use it using setRow. So that's what I've done so far.</p>
<pre><code>byte mask = 128, x;
for (int j = 0; j &lt; 8; j++){
  x = 0;
  for(int p = 0; p &lt; 8; p++){
    x = x | (valueVector[p] &amp; mask);
    if (p != 7)
      x = x &gt;&gt; 1;
  }
  if (j != 7)
  mask = mask &gt;&gt; 1;
  lc.setRow(0, j, x);
}  
</code></pre>
<p>Now i print the rows from row 0 to row 7, from top to bottom. Since I'm printing from top to bottom, this means I have to take the last bit of every column (last bit of every element in vector), and put it in a byte variable. I was thinking to make a byte mask = 128 (b1000000, do a AND logic between to mask and the vector value in order to take the bit I'm looking for, and then making a OR to put the bit in the x byte. After every mask operation, I shift the x variable to the right, in order to make space for the next bit. After i'm done with a row, I shift the mask to the right, in order to take the second bit from the value in the vector. The result I get, is quite odd, I can't explain it, it's not good at all. What I'm i doing wrong with the mask and shifts? I hope I explained well what I'm trying to do. If necessary, I can provide more information. Thanks.</p>
 10285: <p>You use a mask <code>10000000</code>. With each iteration of the outer loop the <code>1</code> in the mask is shifted to the right. <code>01000000</code> -&gt; <code>00100000</code> -&gt; <code>00010000</code>. Let's say we are in the 2nd iteration the mask is <code>01000000</code>. <code>x = x | (valueVector[p] &amp; mask);</code> takes the 2nd bit (counted from left) and sets it at the 2nd position in the x value. But this position contains the bit from the last iteration (as you shifted it to this position).</p>
<p>Possible solution: (I have no compiler at hand. So hopefully it is correct. ;-)</p>
<pre><code>byte x;

for ( int j=0; j&lt;8; j++ )
{
   x = 0;

   for ( int p=0; p&lt;8; p++ )
     x = x | ((valueVector[p] &amp; (1 &lt;&lt; p)) &gt;&gt; p);

   lc.setRow(0, j, x);
}  
</code></pre>
