You must use the numeric AND operation and right-shift to extract the values.
Your header: TTTTLLLLLLLLLLLL
Mask for type: 1111000000000000 = 0xF000
Mask for length: 0000111111111111 = 0x0FFF
Extracting:
// Just an example, this is 0001001000110100 in binary
uint16_t exampleHeader = 0x1234;
// Mask out the length and shift type to the right to get 0001 (binary)
uint8_t type = (exampleHeader & 0xF000) >> 12;
// Same here but no shift needed, returns 001000110100 (binary)
uint16_t length = exampleHeader & 0x0FFF;
Assembling a header:
uint16_t header = (type << 12) | length;
You can replace the integer types (uintXX_t) with the corresponding type of your language, like unsigned long
. The code should be the same for Java and C++, except for the type keywords.