For an embedded System (little Endian ARM, language C - even if I use here for test purpose C++) I wrote the attached piece of code. I have to transmit and receive different configured data, bit aligned and of a given bit length. The configuration is performed using field\_conf\_t for each variable I have to send/receive like:

```
typedef struct field_conf_t { ... }; // see below

typedef struct {
    field_conf_t    a;
    field_conf_t    b;
    field_conf_t    c;
} tx_fields_conf_t;

const tx_fields_conf_t tx_fields_conf = {
    { 0, 8  },      // a - offset/length
    { 28, 12 },     // b - offset/length
    { 56, 8  }      // c - offset/length
};

```

The problem I have is that I think to much about single Bit-Ops, the tests fail. 

Here the code:

```
#include <stdint.h>

#define BOOST_TEST_MODULE BitFieldTest
#include <boost/test/unit_test.hpp>

typedef uint8_t     uint8;
typedef uint16_t    uint16;
typedef uint32_t    uint32;
typedef unsigned long long uint64;

typedef struct {
    uint8   offset;     // [0-63]
    uint8   length;     // [1-32]
} field_conf_t;

typedef struct {
    field_conf_t    a;
    field_conf_t    b;
    field_conf_t    c;
} tx_fields_conf_t;

typedef union {
    uint8   buf[8];
    uint64  contents;
} msg_t;

void set_value(msg_t* const msg, const field_conf_t* const field, uint32 value)
{
    const uint32 mask = (1 << field->length) - 1;
    msg->contents    &= ~(mask          << field->offset);  // clear old contens
    msg->contents    |=  (value & mask) << field->offset;   // set new contens
}

uint32 get_value(const msg_t* const msg, const field_conf_t* const field)
{
    const uint32 mask = (1 << field->length) - 1;

    uint64 value = (msg->contents >> field->offset);
    value       &= mask;

    return (uint32)value;
}

// ########################################################################
struct TestFixture {
    TestFixture() : a(0xAA), b(0xBBB), c(0xCC) {
        conf.a.offset =  0; conf.a.length = 8;
        conf.b.offset = 25; conf.b.length = 12;
        conf.c.offset = 56; conf.a.length = 8;
    }
    uint32 a, b, c;
    msg_t msg;
    tx_fields_conf_t conf;
};

BOOST_FIXTURE_TEST_SUITE(MsgBitfieldTest, TestFixture);

BOOST_AUTO_TEST_CASE(Test_01)
{
    set_value(&msg, &conf.a, a);
    BOOST_CHECK(get_value(&msg, &conf.a) == a);

    set_value(&msg, &conf.b, b);
    BOOST_CHECK(get_value(&msg, &conf.a) == a);
    BOOST_CHECK(get_value(&msg, &conf.b) == b);

    set_value(&msg, &conf.c, c);
    BOOST_CHECK(get_value(&msg, &conf.a) == a);
    BOOST_CHECK(get_value(&msg, &conf.b) == b);
    BOOST_CHECK(get_value(&msg, &conf.c) == c);
}

BOOST_AUTO_TEST_SUITE_END();

```

and the test run output

```
Running 1 test case...
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(65): error in
"Test_01": check get_value(&msg, &conf.b) == b failed
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(69): error in
"Test_01": check get_value(&msg, &conf.b) == b failed
d:/work/bugee/test/can_msg/msg_bitsfields/msg_bitsfields/main.cpp(70): error in
"Test_01": check get_value(&msg, &conf.c) == c failed

*** 3 failures detected in test suite "BitFieldTest"

```

The buffer size is always 8 bytes; I'm happy that I can use 64-bit long long here; before I failed on byte aligned ops also (mask and Bit-Ops on each of the bytes). Note, that the value can not be greater than int32 /uint32. Also I know about C bitfields. They are too slow and I need a fast solution for the Embedded System used (other time consuming tasks are there).

I'm also interested in a solution with lsb/msb 32bit or even 8-byte slice fast solution but I would assume the use of 64 bit datatype is even more performant due to the compiler.