Please explain "TCCR1B |= (1 << CS12);"

KeithRB is correct, but here's a step by step breakdown in case you wanted more detail:

CS12 has a value of 2 since it represents bit 2 of the TCCR1B register.
(1 << CS12) takes the value 1 (0b00000001) and shifts it left 2 times to get (0b00000100).
The order of operations dictates that things in () happen first, so this is done before the "|=" is evaluated.
So now we get TCCR1B |= 0b00000100, which is the same as TCCR1B = TCCR1B | 0b00000100.
Since "|" is "OR", all the bits other than CS12 in TCCR1B are unaffected.

You can do this exact same thing with any of these:

bitSet(TCCR1B, CS12);
TCCR1B |= _BV(CS12);
TCCR1B |= bit(CS12);

I think TCCR1B |= CS12; would work as well, but it's not good practice and may throw a warning.

2 Likes