I have the following code with digitalWrite I aim to write with bitwise operation only. No portability concerns here whatsoever. (for atmega328p here)
#define DATAOUT 11//MOSI
byte commandbits = B11000000;
for (int i = 7; i >= 3; i--) {
digitalWrite(DATAOUT, commandbits & (1 << i));
}
I came up with something like this below which works.
#define DATAOUT 11//MOSI
byte commandbits = B11000000;
for (int i = 7; i >= 3; i--) {
PORTB = (PORTB &(~(1<<3))) | (((commandbits &(1 << i))>>i) << 3);
}
Is this the most concise way. Is there a way to simplify it?