How to read pinMode for digital pin?
There are functions which can get you the registers and bit numbers from a pin number built into the Arduino API.

uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg = portModeRegister(port);

if (*reg &amp; bit) {
    // It's an output
} else {
    // It's an input
}