Shift Register on ATMEGA328p I want to learn more about the shiftout function used with Arduinos and used the implementation with my ATMEGA328p. 

The Setup

This is the original shiftout function used in Arduino Programming, except that I replaced 1 with HIGH. 

#define F_CPU 1000000UL
#define BAUD 9600UL
#define LSBFIRST 0
#define MSBFIRST 1
#define HIGH 0x1
#define LOW  0x0
#define INPUT 0x0
#define OUTPUT 0x1

#include &lt;avr/io.h&gt;
#include &lt;util/delay.h&gt;
#include &lt;USART.h&gt;
#include &lt;pinDefines.h&gt;

#define DATA PD7
#define LATCH PB2
#define CLOCK PB0

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
    uint8_t i;
    for (i = 0; i &lt; 8; ++i) {
        if (bitOrder == LSBFIRST) {
            PORTD |= (!!(val &amp; (HIGH &lt;&lt; i)) &lt;&lt; DATA);
            } else {
            PORTD |= (!!(val &amp; (HIGH &lt;&lt; (7 - i))) &lt;&lt; DATA);
        }
        PORTB |= (HIGH &lt;&lt; clockPin);
        PORTB &amp;= ~(HIGH &lt;&lt; clockPin);
        }
}


Main

int main(void)
{
    initUSART();
    DDRD |= (HIGH &lt;&lt; DATA);
    DDRB |= (HIGH &lt;&lt; LATCH) &amp;&amp; (HIGH &lt;&lt; CLOCK);

    PORTB |= (HIGH &lt;&lt; LATCH);
    shiftOut(DATA, CLOCK, LSBFIRST, 1);
    printBinaryByte(PIND);
    PORTB ^= (HIGH &lt;&lt; LATCH);


    /* Replace with your application code */
    while (1) 
    {


    }

    return (0);
}


I created an example to understand more about the function, but the outcome does not add up to what I expected to see.

Szenario

PORTD |= (!!(val &amp; (HIGH &lt;&lt; 0)) &lt;&lt; DATA);


This is what I expect for val = 1


(HIGH &lt;&lt; 0) = 00000001
val &amp; (HIGH &lt;&lt; 0) = 00000001 &amp; 00000001
!!(val &amp; (HIGH &lt;&lt; 0)) = 1
PORTD |= (1 &lt;&lt; DATA)


As PORTD is the data pin on the shift register, it should get 00000001.
When I print the binary number within PORTD I get: 10000111 and my LED Array connected to the 74HC595 displays: 10101111

I am really lost.... I checked the connections to the LEDs and they are all good. Can you explain to me why I receive this awkward result?
This is what I expect for val = 1
PORTD |= (1 &lt;&lt; DATA)
As PORTD is the data pin on the shift register, it should get 00000001.

your deduction is correct and the code is correct -&gt; remember this is the case for LSBFIRST. so &quot;val = 0x01&quot; should be sent out as &quot;0b1000 0000&quot;, which is precisely what the code is doing.

!(val &amp; (HIGH &lt;&lt; 0))

the &quot;!!&quot; operator is often used to turn a logic value into either '1' or '0'.
the code for shiftout() can be greatly improved.