user16805: 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?

user13432: If you like both portability, readability and performance there are a few libraries (for instance Arduino-GPIO):
#include "GPIO.h"

GPIO<BOARD::D11> data;

// Setup
data.output();

// Manual loop unrolling for speed. Could ask the compiler to do this
// Member function write()
data.write(commandbits & 0x80);
data.write(commandbits & 0x40);
data.write(commandbits & 0x20);
data.write(commandbits & 0x10);
data.write(commandbits & 0x08);

// Or using the assignment operator
data = commandbits & 0x80;
data = commandbits & 0x40;
data = commandbits & 0x20; 
data = commandbits & 0x10;
data = commandbits & 0x08;

And it is as fast as manually written direct port access. 

user4143: Personally I'd expand it out a little:
if (commandbits & (1 << i)) {
    PORTB |= (1<<3);
} else {
    PORTB &= ~(1<<3);
}

It's far more readable, and there are less shifts to mess around with. Plus the AVR has bit set and bit clear operations which that will make use of, especially since it's now using literal values. You could actually replace those values with literals:
if (commandbits & (1 << i)) {
    PORTB |= 0x08;
} else {
    PORTB &= 0xF7;
}


16805: <p>I have the following code with digitalWrite I aim to write with bitwise operation only.
No portability concerns here whatsoever. (for atmega328p here)</p>

<pre><code>#define DATAOUT 11//MOSI 
byte commandbits = B11000000;
for (int i = 7; i &gt;= 3; i--) {
    digitalWrite(DATAOUT, commandbits &amp; (1 &lt;&lt; i));
}
</code></pre>

<p>I came up with something like this below which works.</p>

<pre><code>#define DATAOUT 11//MOSI 
byte commandbits = B11000000;
for (int i = 7; i &gt;= 3; i--) {
    PORTB = (PORTB &amp;(~(1&lt;&lt;3))) | (((commandbits &amp;(1 &lt;&lt; i))&gt;&gt;i) &lt;&lt; 3);
}
</code></pre>

<p>Is this the most concise way. Is there a way to simplify it?</p>
 13432: <p>If you like both portability, readability and performance there are a few libraries (for instance <a href="https://github.com/mikaelpatel/Arduino-GPIO" rel="nofollow noreferrer">Arduino-GPIO</a>):</p>

<pre><code>#include "GPIO.h"

GPIO&lt;BOARD::D11&gt; data;

// Setup
data.output();

// Manual loop unrolling for speed. Could ask the compiler to do this
// Member function write()
data.write(commandbits &amp; 0x80);
data.write(commandbits &amp; 0x40);
data.write(commandbits &amp; 0x20);
data.write(commandbits &amp; 0x10);
data.write(commandbits &amp; 0x08);

// Or using the assignment operator
data = commandbits &amp; 0x80;
data = commandbits &amp; 0x40;
data = commandbits &amp; 0x20; 
data = commandbits &amp; 0x10;
data = commandbits &amp; 0x08;
</code></pre>

<p>And it is as fast as manually written direct port access. </p>
 4143: <p>Personally I'd expand it out a little:</p>

<pre><code>if (commandbits &amp; (1 &lt;&lt; i)) {
    PORTB |= (1&lt;&lt;3);
} else {
    PORTB &amp;= ~(1&lt;&lt;3);
}
</code></pre>

<p>It's far more readable, and there are less shifts to mess around with. Plus the AVR has bit set and bit clear operations which that will make use of, especially since it's now using literal values. You could actually replace those values with literals:</p>

<pre><code>if (commandbits &amp; (1 &lt;&lt; i)) {
    PORTB |= 0x08;
} else {
    PORTB &amp;= 0xF7;
}
</code></pre>
