Direct Port Reading [solved]

I'm trying to read an entire port at once, but for some reason it isn't working.
AD[0-7] are pins 22-29 on the MEGA (PortA)

If I do:

byte Value=0;
//Set PORTA to inputs
DDRA=0x00;
//no pullups
PORTA=0x00;
//Read Inputs
Value=PINA;

Value is always 0xFF

if I change the last line to:

for(int i=0;i<8;i++)
{
  Value = Value|digitalRead(AD[i]<<i);
}

It works fine.

What gives?

if I change the last line to ... It works fine.

Let's take a closer look...

Unrolling the loop and adding some whitespace we get...

Value = Value | digitalRead( AD[0] << 0 );
Value = Value | digitalRead( AD[1] << 1 );
Value = Value | digitalRead( AD[2] << 2 );
Value = Value | digitalRead( AD[3] << 3 );
Value = Value | digitalRead( AD[4] << 4 );
Value = Value | digitalRead( AD[5] << 5 );
Value = Value | digitalRead( AD[6] << 6 );
Value = Value | digitalRead( AD[7] << 7 );

After performing the array lookup and reducing the expression in parenthesis...

Value = Value | digitalRead( 22 );
Value = Value | digitalRead( 46 );
Value = Value | digitalRead( 96 );
Value = Value | digitalRead( 200 );
Value = Value | digitalRead( 416 );
Value = Value | digitalRead( 864 );
Value = Value | digitalRead( 1792 );
Value = Value | digitalRead( 3712 );

Does your board really have a pin 3712?

The second thread in this section is titled Read this before posting a programming question. If you had read that thread, you would have found this item...
Post your complete sketch (program code)!

My bad for the stupid error. The funny thing is that the result from that very wrong loop is what I expected to see, but I know why now.

Also, I know the "Post your complete sketch (program code)!" rule, but in this case it involves thousands of lines across 5 files and isn't really applicable when the problem is obviously in 3 lines of it.

Thanks for finding my facepalmer, it's been a long day with lots of code.

Hillridge:
Also, I know the "Post your complete sketch (program code)!" rule, but in this case it involves thousands of lines across 5 files and isn't really applicable when the problem is obviously in 3 lines of it.

I would rephrase that as "Post a complete sketch that demonstrates the problem". If your actual sketch contains a significant amount of code not relevant to the problem, write a test sketch to demonstrate the problem in the simplest possible way. Most likely, the very act of doing that will reveal a false assumption or enable you to spot the problem for yourself; it's a very useful investigative technique, even if you aren't planning to ask for help.