Rather than read and write the ports bit by bit, you can use "direct port manipulation". For example, all of your code that transfers the contents of variable a to PORTD bit by bit can be done in just one line as follows:
PORTD = a;
Likewise, you can read PORTD in parallel and transfer its contents to a variable a as follows:
a = PIND;
Of course, for those operations, all the pins of PORTD have to be set to input or output, as appropriate. See Arduino Reference - Arduino Reference