# MULITPLEXER HELP

http://www.arduino.cc/playground/Learning/4051

I am reading this example on the playground, I don’t quite understand the code in the loop section

void loop () {

for (count=0; count<=7; count++) {

row = bin[count];

r0 = row & 0x01;

r1 = (row>>1) & 0x01;

r2 = (row>>2) & 0x01;

digitalWrite(2, r0);

digitalWrite(3, r1);

digitalWrite(4, r2);

//Serial.println(bin[count]);

delay (1000);

}

what does the r1 = (row>>1) & 0x01;

r2 = (row>>2) & 0x01;

mean? is that adding 1 to the r0 value? or is it a pointer to the next value? Ive never seen the >> used in any code. and also what does &0X01 mean?

any help would be appreciated to deepen my understanding. thanks

It read as number between 0 and 7 from array bin and then sets the port 2 to the rightmost bit, port 3 to the second rightmost bit and port 4 to the third rightmost bit by shifting out the bits already processed with the >> operator and clearing all except the rightmost bit with the bitwise and operator (&).

Look up your C-tutorial page about bitwise operators. Once you understand that, the code in this example will be very obvious.

Korman

thanks for the replies, I read a C programming link and that tutorial however I am still confused :-/

okay, say we run through the for loop starting with count = 0, I'm assuming the first address will be r0 r1 r2 = 000 to select the first pin of the MUX

row = bin //which means that row = 000?
r0 = 000 & 001 // doesn't this equal 000? I thought it was supposed to equal 0?
r1 = (row>>1) & 0x01 //still 000 right?
r2 = (row>>1) //

wouldn't it write 000 000 000?

then when count = 1
row = bin; //row = 1?
r0 = 1 & 001? // how can you compare these two?

any help running through the for loop would be greatly appreciated.

For 000 it would write r0=0, r1=0 and r2=0.
For 001 it would write r0=1, r1=0 and r2=0.
For 011 it would write r0=1, r1=1 and r2=0.

Korman

Right, I understand how addressing for multiplexers work and how binary numbers are represented, but I'm just lost in the code in this example.

Binary numbers are just numbers like any other. It doesn't matter how you write them (0b010 or 0x2 or 2). To understand what the bitwise operators >> and & do and to understand why this is done in this code, best run the 3 lines of code on paper with a pen. The number 0 is a really bad example because nothing happens use 3 (binary 011) or 5 (binary 101). Then you'll see how the bit move and get cleared.

Korman