Creating a function to address arbitrary pins on two multiplexers

I am not sure if this is the write section as my question is more related to programming.

So, currently I am trying to control two multiplexer with an Arduino mega. I am using port manipulation to write the address to my two multiplexers to receive data from the selected pin.(I attached a picture of the relevant part of the data sheet). The current code I am using makes a seperate function for each address I write to the multiplexers. This is not efficient for what I am doing as I am taking 9 inputs into the multiplexers and eventually want to expand to 16 inputs. To do this would require a large number of functions. Here is the code I am currently using for just 3 inputs. I have attached the relevant piece of code below. I am using pins 22-30 on the Arduino mega which corresponds to port A when using port manipulation.

void setup() {
  DDRA = DDRA | B11111111; //22-30 as output


void loop() {



void read1and2() {
  PORTA = B00010000; // all low select pin 1 on first mux and 2 on second mux
  t1 = micros();
  data1 = analogRead(54);

void read2and3() {
  PORTA = B00100001; //write pin 22 and 27 select pins 2 and 3 on muxes
  t2 = micros();
  data2 = analogRead(54);

void read1and3() {
  PORTA = B00100000; //write pin 27 select pins 1 and 3 on muxes
  t3 = micros();
  data3 = analogRead(54);

What I was thinking of doing was creating a single function that took two inputs corresponding to the two inputs that I want to choose. I would then have an array for each pin that has the address it needs to write. The use the PORT to select using the array. However, I am not sure of how to actually implement it. I put some code that hopefully shows the idea of what I am trying to do. Obviously this code works but is suppose to give an idea of what I am trying to accomplish.

void setup() {
  // put your setup code here, to run once:
  array[] = {0000,0001,0010,0011,0100,0101,0110,0111,1000};

void loop() {
  // put your main code here, to run repeatedly:

void chooseInput(int mux1, int mux2){
    PORTA = Barray[mux2]array[mux1]

If has an idea on the implementation or has another completely different idea to reduce all of these calls to a single function that just needs the two input pins you want I would appreciate any idea.

What I think you are missing is the concept of bit shifting to manipulate bit patterns. The << operator is shift to the left, and the | operator in the inclusive OR operation. These are the two concepts you will need.

Suppose you want to select two addresses that means you need to write a bit pattern to your port with two bits set to one and all the others at zero. So you take those two numbers and turn them into a bit pattern to write to your port:-

void select(byte input1, byte input2){
PORTA = 1<< input1 | 1<< input2;