Understanding how 74HC4051 multiplexer selects pins

I have recently started implementing multiplexers into my projects. After following a tutorial posted by VRomanov89 on youtube, I understand how the 74HC4051 selects pins using binary logic from three digital pins. The only thing I don’t understand is what is actually going on in the sample code he provided. It all works, but it’s where he begins to use the digitalWrite functions with binary operators to select mux input pins that I get lost.

Here is the sample code:

int pin_Out_S0 = 7;
int pin_Out_S1 = 8;
int pin_Out_S2 = 9;
int pin_In_Mux1 = A3;
int Mux1_State[8] = {0};

void setup() {
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);
  //pinMode(pin_In_Mux1, INPUT);

void loop() {
  for(int i = 0; i < 8; i ++) {
    if(i == 7) {
    } else {

void updateMux1 () {
  for (int i = 0; i < 8; i++){
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
    Mux1_State[i] = analogRead(pin_In_Mux1);

I understand everything in the code except for his digitalWrite statements in the updateMux1 function. I understand that by adding “&& (i & B00000001)” he is causing the digital pins to count from 0 to 7 and thus be able to read all of the mux input pins in sequence. Can somebody explain that simple line of code? The “&&” symbol throws me off. I understand “&&” to be the “and” logical operator. If that’s what it is in this context, how is it possible that he is running a logic operation within a call to another function?

I’ve used his code in some of my own projects and it works fine, but I need to understand WHY it works. What is happening in the code? Thank you in advance, anyone who may enlighten me.

TL;DR: please explain what is happening in the sample code once the arduino executes the three digitalWrite lines.

All you have to do is to do some Serial prints of those values.

"HIGH && x" is a truly awful idiom for "x != 0". Gack!

HIGH && is [u]so[/u] unnecessary.

HIGH && is so unnecessary.

So how would you write it? I'm very confused as to how you would cleanly write it so that it would read each pin on the multiplexer.

Perhaps like this :) ( I am not an expert)

    digitalWrite(pin_Out_S0, i & B00000001);
    digitalWrite(pin_Out_S1, i & B00000010);
    digitalWrite(pin_Out_S2, i & B00000100);