This code I found on this forum is for a multiplexer.
for (int i=0; i<16; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
//See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
//See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
digitalWrite(CONTROL0, (i&15)>>3); //S3
digitalWrite(CONTROL1, (i&7)>>2); //S2
digitalWrite(CONTROL2, (i&3)>>1); //S1
digitalWrite(CONTROL3, (i&1)); //S0
.........
I looked up the references for bitwise and, and bitshift. I seem to understand how they function. Bitwise AND only returns the bits where values that are compared shared a 1, and bitshifting just shifts the bits left or right.
I am having a hard time understanding what this code is doing though. I get that 15,7,3, and 1 all represent a binary number consisting of all 1's (00001111, 00000111, 00000011, and 00000001). So then what is this code actually doing? Why do you need to compare 0&15 and bitshift it to the right 3 times?
What does one iteration of this loop do?
Thanks
edit: I started working out what values would be returned in a given iteration. So for instance given i=5
5&15>>3
00000101
00001111
00000101 >> 3 = 00000000
5&7>>2
00000101
00000111
00000101 >> 2 = 00000001
5&3>>1
00000101
00000011
00000001 >> 1 = 00000000
5&1
00000101
00000001
00000001 >> 0 = 00000001
while i=6 gives 0, 1, 1, 0 respectively. So I guess the ultimate purpose here is to iterate all values of 0's and 1's (meaning HIGH/LOW in this case?). Quite a confusing way to read it in the code but I suppose it makes a lot of sense once you understand its purpose.