#include <iostream>
#include <cstdint>
#include <bitset>
#include <cstring>
#include <algorithm>
struct bits
{
unsigned sender : 16 ;
unsigned res : 16 ;
unsigned e : 12 ;
unsigned f : 4 ;
};
int main()
{
const bits b { 0xffff, 3, 2, 1 } ;
const std::uint32_t res = b.res ;
const std::uint32_t e = b.e ;
const std::uint32_t f = b.f ;
std::uint32_t buff = ( res << 16 ) | ( e << 4 ) | f ;
// test it
std::cout << std::bitset<32>(buff) << '\n' ;
// copy it
std::uint8_t byte_buff[4] ;
std::memcpy( byte_buff, &buff, sizeof(buff) ) ;
// check endianness
for( std::uint8_t b : byte_buff ) std::cout << std::bitset<8>(b) << ' ' ;
std::cout << '\n' ;
// if needed, swap bytes
std::swap( byte_buff[0], byte_buff[3] ) ;
std::swap( byte_buff[1], byte_buff[2] ) ;
for( std::uint8_t b : byte_buff ) std::cout << std::bitset<8>(b) << ' ' ;
std::cout << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGludD4KI2luY2x1ZGUgPGJpdHNldD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CgpzdHJ1Y3QgYml0cwp7CiAgICB1bnNpZ25lZCBzZW5kZXIgOiAxNiA7CiAgICB1bnNpZ25lZCByZXMgOiAxNiA7CiAgICB1bnNpZ25lZCBlIDogMTIgOwogICAgdW5zaWduZWQgZiA6IDQgOwp9OwoKaW50IG1haW4oKQp7CiAgICBjb25zdCBiaXRzIGIgeyAweGZmZmYsIDMsIDIsIDEgfSA7CgogICAgY29uc3Qgc3RkOjp1aW50MzJfdCByZXMgPSBiLnJlcyA7CiAgICBjb25zdCBzdGQ6OnVpbnQzMl90IGUgPSBiLmUgOwogICAgY29uc3Qgc3RkOjp1aW50MzJfdCBmID0gYi5mIDsKCiAgICBzdGQ6OnVpbnQzMl90IGJ1ZmYgPSAoIHJlcyA8PCAxNiApIHwgKCBlIDw8IDQgKSB8IGYgOwogICAgLy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