Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
union_bitfield_cpp_issue.cpp
#include <iostream>
#include <cstdint>
#include <arpa/inet.h>
// g++ ../isolated_example.cpp -std=c++11
typedef union __attribute__((__packed__)) {
uint16_t reserved_flag : 1, dont_fragment_flag : 1, more_fragments_flag : 1, fragment_offset : 13;
uint16_t fragmentation_details_as_integer;
} fragmentation_details_t;
static_assert(sizeof(fragmentation_details_t) == 2, "Bad size for fragmentation_details_t");
int main() {
fragmentation_details_t test_union;
test_union.fragmentation_details_as_integer = 64; // UINT16_MAX;
test_union.fragmentation_details_as_integer = ntohs(test_union.fragmentation_details_as_integer);
std::cout << "reserved_flag: " << test_union.reserved_flag << " dont_fragment_flag: " << test_union.dont_fragment_flag
<< " more_fragments_flag: " << test_union.more_fragments_flag << " fragment_offset: " << test_union.fragment_offset << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.