verify my understanding of direct pin manipulation

i made a program to read multiple hard drive style encoders using digitalRead, but someone reccomneded to use direct pin manipulation ast it would be faster. here are some excerpts of the code that i want to make sure im doing right, could someone look over it quick?

  //goes from left to right
  //       76543210
  DDRD =  B00000000; //sets 2 through 7 as inputs, RX and TX stay as inputs
  PORTD &= B11111100; //enable pullups on all but tx/rx pins
  
  //       ..13...8
  DDRB &= B11000000; //sets through 13 as inputs excpet for crystal pins
  PORTB &= B00111111; //enable pullups on all but crystal pins
  
   //     ..543210 
  DDRC = B00000000; //sets analog 0 through 5 as inputs
  PORTC &= B00111111; //enable pullups on all but analog 6 an 7 (6 and 7 arent avaliable on atmega 328)
 //         76543210
  //(PIND & B00001000) check only the pin '1' (in this case digital 3) in this line for low or high, & means dont check any others
  //would be the same as: PIND & (1 << 3);

if ((PIND & B00001000) == 1) //check state of digital pin 3, if it is HIGH
    {
    }

if ((PIND & B00001000) == 0) //check state of digital pin 3, if it is LOW
    { 
    }

also, which would be faster:

if ((PIND & B00001000) == 1) //check state of digital pin 3, if it is HIGH
    {
    }

or

if ((PIND & (1 << 3)) == 1) //check state of digital pin 3, if it is HIGH
    {
    }
//         76543210
  //(PIND & B00001000) check only the pin '1' (in this case digital 3) in this line for low or high, & means dont check any others
  //would be the same as: PIND & (1 << 3);

The value of (PIND & B00001000) will be B00001000 (true) if bit 3 of PIND is set, and B00000000 (false) if it is not - and yes, the two representations are equivalent. There is a reasonable probability that the compiler will replace the shift expression with a logical value no matter how you code it.

if ((PIND & B00001000) == 1) //check state of digital pin 3, if it is HIGH
 {
 }

No, if pin 3 is high the value of the expression will be 8 (true) which will not == 1

if ((PIND & B00001000) == 0) //check state of digital pin 3, if it is LOW
{ 
}

Yes.

if ((PIND & B00001000) == 1) //check state of digital pin 3, if it is HIGH
    {
    }

if ((PIND & (1 << 3)) == 1) //check state of digital pin 3, if it is HIGH
    {
    }

If the compiler were brain-dead, the first example would be faster (but wrong) because it starts with a value that the second example would waste time constructing with a shift - but gcc isn't in the brain-dead catagory, so I'd be willing to bet a cup of coffee that both examples will compile to the exact same code.

Note that it's not necessary to compare the result of the AND to 1. Anything non-zero will be true, and only a zero result will be false.

can you link me to, or explain why it would be 16?

thanks

No I can't. :smiley:

you seemed to know when you answered my first question...

but i think this is right: 1111111 checking for true would be 128+64+32+16+8+4+2+1

Late night brain fart - which was immediately corrected with an edit. If you need an apology from me for that, consider it offered. I'll be more careful in the future...

so if i want to check fro true i can just do this:
if ((PIND & B00001000))
?

but i figured the true will not always be one thing: 1111111 checking for true would be 128+64+32+16+8+4+2+1