syntax question

Hello I've been looking into making an LED cube and I ran across I line of code and I'm curious if you can explain a part of it.

digitalWrite( Pins[index] , 7 & (1 << 0) );

What is the meaning of adding the "7 & (1<<ledcol)"? I don't understand what this comparison does.
ledcol goes from 0 to 8.

Thank you!

It's not a comparision, << is a bit shift operator. And & is the bit "and" operator. Arduino Playground - BitMath

I can't quite get the meaning of the one line of code you posted though. Perhaps if you posted more of the surrounding code?

Cheers,
John

// display pattern in table until DisplayTime is zero (then repeat)
void loop()
{
  // declare variables
  byte PatternBuf[ PLANESIZE ];      // saves current pattern from PatternTable
  int PatternIdx;
  byte DisplayTime;        // time*100ms to display pattern
  unsigned long EndTime;
  int plane;      // loop counter for cube refresh
  int patbufidx;   // indexes which byte from pattern buffer
  int ledrow;    // counts LEDs in refresh loop
  int ledcol;    // counts LEDs in refresh loop
  int ledpin;    // counts LEDs in refresh loop

  // Initialize PatternIdx to beginning of pattern table
  PatternIdx = 0;
  // loop over entries in pattern table - while DisplayTime>0
  do {
    // read pattern from PROGMEM and save in array
    memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );
    PatternIdx += PLANESIZE;
    // read DisplayTime from PROGMEM and increment index
    DisplayTime = pgm_read_byte_near( PatternTable + PatternIdx++ );    
    // compute EndTime from current time (ms) and DisplayTime
    EndTime = millis() + ((unsigned long) DisplayTime) * TIMECONST;

    // loop while DisplayTime>0 and current time < EndTime
    while ( millis() < EndTime ) {
      patbufidx = 0;    // reset index counter to beginning of buffer
      // loop over planes
      for (plane=0; plane<CUBESIZE; plane++) {
        // turn previous plane off
        if (plane==0) {
          digitalWrite( PlanePin[CUBESIZE-1], HIGH );
        } else {
          digitalWrite( PlanePin[plane-1], HIGH );
        }

        // load current plane pattern data into ports
        ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
          patbufidx++;
        } 

        // turn current plane on
        digitalWrite( PlanePin[plane], LOW );
        // delay PLANETIME us
        delayMicroseconds( PLANETIME );
      }    // for plane
    }    // while <EndTime
  } while (DisplayTime > 0);        // read patterns until time=0 which signals end

mchammer9111:

     // load current plane pattern data into ports

ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
          patbufidx++;
        }




The digitalWrite here is going through each bit of the current location in the PatternBuff to decide whether to turn the corresponding LED on (1) or off (0).

The (1 << ledcol) is the "mask" which would specify which bit in PatternBuf[patbufidx] to look at.

HTH?
John

so I'm getting numbers like B111 and B100 and my cube is 3x3x3 so wouldnt it need more bits to represent all 9 led's?

mchammer9111:
so I'm getting numbers like B111 and B100 and my cube is 3x3x3 so wouldnt it need more bits to represent all 9 led's?

Which 9 leds?

Sorry, let me put it differently. ledcol only varies from 0 to 2. There are only 3 "columns"...

ok I'm confused about the bit shifting. So I traced 9 iterations of the loop and I don't see whats happening.

        // load current plane pattern data into ports
        ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
        }

Here's where I'm confused.
B0011 & (1<<0) => B011 & 1
B0011 & (1<<1) => B011 & 10
B0011 & (1<<2) => B011 &100

How do I know when the pin is in the high/low state? I'm assuming B000 & 1 would be in the low state. I guess I don't understand the AND operator.

If you digitalWrite a non-zero value, the pin will be on.

Are you sure about that last result ?

B0011 & (1<<2)=B100

The result should be B000

You know when a particular bit is high because the result of the bitwise AND is not zero
Run this to see how

byte test = B1010;

void setup() 
{
  Serial.begin(9600); 
 Serial.println(test,BIN); 
  for (int a = 0;a <= 3;a++)
  {
    int x = test & (1 << a);
    Serial.print(a);
    Serial.print("\t");
    Serial.print(x ,BIN);
    Serial.print("\tbit ");
    Serial.print(a);

    if (x == 0)
    {
      Serial.println("\tis not set");       
    }
    else
    {
      Serial.println("\tis set");
    }
  }
}

void loop() {}

ok thanks guys. I finally understand how this works haha. You're all awesome!