Alternative code for pinMode()?
It isn't worth it for something done once, if you only do it in setup. However if you need to toggle the mode frequently yes it can be done. You sadly haven't said which pins, but assuming they are D2 to D6 (digital pins 2 to 6) on a Mega you can do this to set output mode:

  DDRE |= bit(PE3) | bit(PE4) | bit(PE5); // D5, D2, D3
  DDRG |= bit(PG5); // D4
  DDRH |= bit(PH3); // D6


For input mode you have to "and" in the ones-complement of the bits like this:

  DDRE &amp;= ~(bit(PE3) | bit(PE4) | bit(PE5)); // D5, D2, D3
  DDRG &amp;= ~(bit(PG5)); // D4
  DDRH &amp;= ~(bit(PH3)); // D6


The exact register names are in the datasheet, and the mappings to the pins on your board are in the schematic (available from the Arduino site). The relevant part is this:



The bit numbers are in grey on the left (eg. PE4) and the board pin numbers are in green on the right (eg. 2 is digital pin 2).

The bit macro works similarly to the _BV one you mentioned in your question. It shifts 1 left the required number of bit positions to be in the right place.



A somewhat simpler and more portable solution is to use the digitalWriteFast library which you can download from:

https://github.com/NicksonYap/digitalWriteFast

This uses various compiler tricks to detect if you are using a constant for pinMode and digitalWrite and converts them into the appropriate register manipulation. You just install the library and add Fast to the function names, eg.

pinModeFast (2, OUTPUT);
digitalWriteFast (2, HIGH);