I'm programming network headers and a lot of protocols use 4 bits fields. Is there a convenient type I can use to represent this information?
The smallest type I've found is a BYTE. I must then use a lot of binary operations to reference only a few bits inside that variable.
A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte. Given a byte, swap the two nibbles in it. For example 100 is be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100).
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits. The type int should be the integer type that the target processor is most efficiently working with. This allows great flexibility: for example, all types can be 64-bit.
Since the memory is byte-addressed, you can't address any unit smaller than a single byte. However, you can build the struct
you want to send over the network and use bit fields like this:
struct A {
unsigned int nibble1 : 4;
unsigned int nibble2 : 4;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With