Say var is either 1 or 0 and I want to copy its value to PB3 in DDRB. Also, I don't know or want to change any of the other bits in DDRB.
Whats the best way of doing this?
Some of the ways I've come up with are:
if(!(DDRB & (var << PB3))) DDRB |= (1 << PB3);
// Clear then set to var DDRB &= ~(1 << PB3); DDRB |= (var << PB3);
DDRB = (DDRB&~(1 << PB3))|(var << PB3);
Method 1) uses a conditional and requires reading the current value of DDRB.
Method 2) doesn't need to read DDRB, but it does a redundant masking.
Method 3) reads DDRB and is a bit tricky to understand.