https://github.com/mrk21/bitfield
C++11 bitfield template library
https://github.com/mrk21/bitfield
cpp cpp11
Last synced: 2 months ago
JSON representation
C++11 bitfield template library
- Host: GitHub
- URL: https://github.com/mrk21/bitfield
- Owner: mrk21
- License: mit
- Created: 2014-04-02T05:00:48.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-07-09T08:55:11.000Z (about 11 years ago)
- Last Synced: 2025-04-02T17:22:37.133Z (3 months ago)
- Topics: cpp, cpp11
- Language: C++
- Homepage:
- Size: 414 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
bitfield
========C++11 bitfield template library
Examples
========Analyzing the IP header:
```c++
#include
#include// IP header
union ip_header {
using version_type = bitfield::field< 4>;
using ihl_type = version_type::next_field< 4>;
using dscp_type = ihl_type::next_field< 6>;
using ecn_type = dscp_type::next_field< 2>;
using total_length_type = ecn_type::next_field<16>;
using identification_type = total_length_type::next_field<16>;
using flags_type = identification_type::next_field< 3>;
using fragment_offset_type = flags_type::next_field<13>;
using time_to_live_type = fragment_offset_type::next_field< 8>;
using protocol_type = time_to_live_type::next_field< 8>;
using header_checksum_type = protocol_type::next_field<16>;
using source_ip_address_type = header_checksum_type::next_field<32>;
using dest_ip_address_type = source_ip_address_type::next_field<32>;
using options_type = dest_ip_address_type::next_field<32>;
using container_type = options_type::container_type;
container_type container;
version_type version;
ihl_type ihl;
dscp_type dscp;
ecn_type ecn;
total_length_type total_length;
identification_type identification;
flags_type flags;
fragment_offset_type fragment_offset;
time_to_live_type time_to_live;
protocol_type protocol;
header_checksum_type header_checksum;
source_ip_address_type source_ip_address;
dest_ip_address_type dest_ip_address;
options_type options;
};int main() {
// packet
ip_header packet{{{
0x45, 0x00, 0x00, 0x30, 0x26, 0xd6, 0x40, 0x00, 0x80, 0x06, 0x58, 0x86, 0xc0, 0xa8, 0x01, 0x8f,
0xd2, 0xe1, 0xe6, 0x52,
}}};
//== Get field ==
// IP version: 4
// total length: 48
std::cout << "IP version: " << packet.version << std::endl;
std::cout << "total length: " << packet.total_length << std::endl;
//== Set field ==
// TTL: 128
// TTL: 127
std::cout << "TTL: " << packet.time_to_live << std::endl;
packet.time_to_live = packet.time_to_live - 1; // decrement TTL
std::cout << "TTL: " << packet.time_to_live << std::endl;
return 0;
}
```