Trying to understand port registers...

I found this code online:

for (i=0; i<8; i++){
    PORTB = (PORTB & B00001111) | (i<<4);
}

I am just trying to understand what the incrementing 'i' does in this case. Is it counting 0000,1000,0100,1100, etc.

As i increments from 0 to 7, the code sets the top four bits of port B to those values.

The actual result depends on how the port is configured.

i counts 0, 1, 10, 11, 100...

Then it is left-shifted by 4: 0, 10000, 100000, 110000, 1000000...

This loop is going to execute so fast that you may as well just set i to 7 and run it once.

If you examine the binary representations of i as it cycles from 0 to 7,
0000
0001
0010
0011
0100
0101
0110
0111
each time through the loop the 4 msbs are clear to zero (PORTB & 0B00001111) and the set with the value of i (i << 4), so it's effectively cycling the pins attached to the high order nibble on and off (assuming they're configured as OUTPUT). This doesn't make sense on a 328p processor as there are no pin connections to bits 6 and 7 on PORTB. Actually the code doesn't make much sense as it would happen so quickly you wouldn't notice. Perhaps connected to some peripheral controller? More code or an example of where it's being used would be helpful. If it's just a learning exercise well, there you have it.

DKWatson:
If you examine the binary representations of i as it cycles from 0 to 7,
0000
0001
0010
0011
0100
0101
0110
0111
each time through the loop the 4 msbs are clear to zero (PORTB & 0B00001111) and the set with the value of i (i << 4), so it's effectively cycling the pins attached to the high order nibble on and off (assuming they're configured as OUTPUT). This doesn't make sense on a 328p processor as there are no pin connections to bits 6 and 7 on PORTB. Actually the code doesn't make much sense as it would happen so quickly you wouldn't notice. Perhaps connected to some peripheral controller? More code or an example of where it's being used would be helpful. If it's just a learning exercise well, there you have it.

This is a Leonardo board, I have output pins on 8,9,10,11 for a 16x mux. So is this going to poll through the first 8 channels on this board? That is what it appears to be doing but I just wanted to understand what it was doing exactly.

pin11 never gets changed if that's an issue. What mux?

for (i=0; i<8; i++){

PORTB = (PORTB & B00001111) | (i<<4);
}

This preserves the previous state of the low bits PORTB.
It might be clearer written like:

uint8_t oldPortB = PORTB;
oldPortB &= 0xF;  // save just the bits we want to keep.
for (i=0; i<8; i++){
    PORTB = oldPortB | (i<<4); // put the count in the top bits and output
}
i :    port
0 : 0b0000xxxx
1 : 0b0001xxxx
2 : 0b0010xxxx
3 : 0b0011xxxx
4 : 0b0100xxxx
5 : 0b0101xxxx
6 : 0b0110xxxx
7 : 0b0111xxxx

'x's added as per westfw - thanks.

Except it won't be 0s in the low four bits - it will be whatever was there before.