Fastest way to extract n bytes from uint8_t buffer I work a lot with byte buffers and have to extract different parts. In this example, it's 4 byte, but it ranges from a single bit to 128 bit. Speed is the most important metric here. See the code for a MWE. I'd like to know if there is a better way.

#include &lt;stdint.h&gt;

static uint32_t get_data(uint8_t *buf, size_t off)
{
    return ((uint32_t)(buf[off + 0]) &lt;&lt; 24) +
                  ((uint32_t)(buf[off + 1]) &lt;&lt; 16) +
                  ((uint32_t)(buf[off + 2])  &lt;&lt;  8) +
                  ((uint32_t)(buf[off + 3]));
}

int main(int argc, char **argv)
{
    uint8_t buf[128];

    /* get some example data */
    for (uint8_t i = 0; i &lt; 128; ++i)
        buf[i] = i;

    /* we want the data from offset 10 as an uint32_t */
    uint32_t res = get_data(buf, 10);
}
Since you want low level operations, I'd suggest memmove

#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

static uint32_t get_data(uint8_t *buf, size_t off)
{
    return ((uint32_t)(buf[off + 0]) &lt;&lt; 24) +
                  ((uint32_t)(buf[off + 1]) &lt;&lt; 16) +
                  ((uint32_t)(buf[off + 2])  &lt;&lt;  8) +
                  ((uint32_t)(buf[off + 3]));
}

int main(int argc, char **argv)
{
    uint8_t buf[128];

    /* get some example data */
    for (uint8_t i = 0; i &lt; 128; ++i)
        buf[i] = i;

    clock_t t = clock();
    uint32_t res;
    for(int i=0; i&lt;10000; i++)
        memmove(&amp;res, buf+10, sizeof(uint32_t));
    t = clock() -t;
    printf("Time %lf\n", (double)t/CLOCKS_PER_SEC);

    t = clock();
    for(int i=0; i&lt;10000; i++)
     res = get_data(buf, 10);
    t = clock() -t;
    printf("Time %lf\n", (double)t/CLOCKS_PER_SEC);
}


Because a single copy doesn't show any difference I tried with 10.000 and my results were: 

Time 0.000049
Time 0.000090


Almost double the speedup


EDIT 1: As mentioned in the comments, memcpy is a viable alternative
to memmove. 
EDIT 2: The speed difference in this example cannot be
observed with -O flag as the compiler executes the loop only one
time.