Explaining bitwise operation

Can someone try and explain me or direct me to sites that do this following part of code:

for (int i = 0; i < 15; i++) {
    
    digitalWrite(s0, (i & 1));
    digitalWrite(s1, (i & 2) >> 1);
    digitalWrite(s2, (i & 4) >> 2);
    digitalWrite(s3, (i & 8) >> 3);
}

I understand the the result of the function is translating integer numbers to thier corresponding bit numbers(?)
I don't understand how is happening..

Tutorial:
Review & come back if needed...

Arduino Playground - BitMath

have a look at bitwise operators

You can eliminate the right shift operators in the code you posted, and it will work just as well.

I understand the operators:

(i & 1)
(i & 2)
i & 4)
(i & 8)

when i = 3 for example

i 0000 0011
1 0000 0001

result is 1 at s1. (lsb)

i 0000 0011
2 0000 0010

result is 1 at s2

i 0000 0011
4 0000 0100

result is 0 at s3.

i 0000 0011
8 0000 1000

result is 0 at s4.

when i = 7

i 0000 0111
1 0000 0001

result is 1 at s0

i 0000 0111
2 0000 0010

result is 1 at s1

i 0000 0111
4 0000 0100

result is 1 at s2.

i 0000 0111
8 0000 1000

result is 0 at s3.

I just can't understand the shifting part.

The shifting part simply moves the bit to the least significant bit position.
This isn't necessary in the case of digitalWrite.

It's equivalent to this

digitalWrite(s0, (i & 1));
digitalWrite(s1, (i >> 1) & 1);
digitalWrite(s2, (i >> 2) & 1);
digitalWrite(s3, (i >> 3) & 1);

and also to this?

digitalWrite(s0, (i & 1));
digitalWrite(s1, (i & 2));
digitalWrite(s2, (i & 4));
digitalWrite(s3, (i & 8));

?

No.

why is there? if it is not necessary how come is not badly effect the operation?

Because whoever wrote it wanted it to be "correct".

when i = 3 for example

i 0000 0011
1 0000 0001

result is 1 at s1. (lsb)

i 0000 0011
2 0000 0010

result is 1 at s2

i 0000 0011
4 0000 0100

result is 0 at s3.

i 0000 0011
8 0000 1000

result is 0 at s4.

when i = 7

i 0000 0111
1 0000 0001

result is 1 at s0

i 0000 0111
2 0000 0010

result is 1 at s1

i 0000 0111
4 0000 0100

result is 1 at s2.

i 0000 0111
8 0000 1000

result is 0 at s3.

is this not true? without the shifting part

For some values of !yes

or

int pins[] = {s0, s1, s2, s3};
for (int i = 0; i < 15; i++) {
    for (int b = 0; b < 4; b++) {
    digitalWrite(pins[b], bitRead(i, b) ? LOW : HIGH );
  }
}

Technically, the interpretation of non-zero for HIGH, is not part of the API. It's just fortuitous. Also, taking the value of HIGH == 1. There is no guarantee that it will always be like that, it's only extremely likely.

But it's not, because it's still assuming HIGH==1
"Correct" would be like:

    digitalWrite(s2, (i & 4) ? HIGH:LOW);

It might be faster, too. multi-bit shifts on an AVR are slow. (sigh. It isn't)

Shoot, I always mix up the "sense" of the ternary op. :slight_smile: True comes first.

If someone complains that the previous solution also suffers from an assumption of non-zero==true, no, because it is defined for boolean expressions in the language.

Hence the " "correct" " :wink:

If we defined

byte a = 0b00010000;

a >> 1 will be 0b00001000
a >> 2 will be 0b00000100
a >> 3 will be 0b00000010
a >> 4 will be 0b00000001