#include <util/atomic.h>
// https://forum.arduino.cc/t/manipulate-multiple-pins-with-portd/974951/15?u=dougp
void setup() {
// suggested by @westfw
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uint8_t tempv = PORTD & ~0xF0;
PORTD = tempv | 0xC0;
}
noInterrupts();
PORTD &= ~0b11110000; // clear bits 7-4, bits 3-0 unchanged
PORTD |= 0b11000000; // set bits 7 and 6, bits 5-0 unchanged
interrupts();
// suggested by @johnwasser
byte DesiredValue = 0b1010;
PIND = (PORTD ^ (DesiredValue << 4)) & 0b11110000;
}
void loop() {
}