Failing when reading PORT on Arduino Uno with binary operators

I've been sitting here scratching my head for a while now. I was trying to read pin 3 in PORTD with binary operators instead of with digitalRead. So, here is a working example using digitalRead (pin 3 is physically high in both cases):

void setup() {
  Serial.begin(9600);
  DDRD = 0b00000000;
  PORTD = 0b00000000;
}
void loop() {
  if (digitalRead(3) == HIGH)
    Serial.println("working");
}

And here is the replacement (not working) which I thought would work equally well:

void setup() {
  Serial.begin(9600);
  DDRD = 0b00000000;
  PORTD = 0b00000000;
}
void loop() {
  if (PORTD & (1<<3)) // Have tried (1<<2) as well, just in case.
    Serial.println("working");
}

What have I missed?

I'm not used to doing port manipulation, but this link says that

PORTD is the register for the state of the outputs. For example;

PORTD = B10101000; // sets digital pins 7,5,3 HIGH

You will only see 5 volts on these pins however if the pins have been set as outputs using the DDRD register or with pinMode().

PIND is the input register variable It will read all of the digital input pins at the same time.

So if you want to read pin 3, you should lok at PIND.

Another tutorial here says

Now to use the I/O pins as inputs. Again, it is very simple to do so. In void setup(), we use

DDRy = Bxxxxxxxx

where y is the register type (B/C/D) and xxxxxxxx are eight bits that determine if a pin is to be an input or output. Use 0 for input. The LSB (least-significant bit [the one on the right!]) is the lowest pin number for that register. Next, to read the status of the pins we simply read the byte:

PINy

where y is the register type (B/C/D). So if you were using port B as inputs, and digital pins 8~10 were high, and 11~13 were low, PINB would be equal to B00000111. Really, that’s it!

You missed the semicolon on one of your lines :slight_smile:

Thank you for the help, lesept!

Where is the missing semicolon, noweare? Maybe I'm just too sleepy to see it.

My bad, I think I'm the sleepy one. No missing semi.

Please post your code when you get the expected result, I'll be interested

lesept:
Please post your code when you get the expected result, I'll be interested

I just changed PORTD & (1<<3) into PIND & (1<<3)

So simple ! Thx