i am looking at the CoAP implementation in ContikiOS, particularly at the header parsing and i am having some trouble understanding two operations. The code is bellow.
coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_VERSION_POSITION;
coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0]) >> COAP_HEADER_TYPE_POSITION;
The coap_pkt the structure that houses the packet and the parsed values.
- the version is an uint8_t (unsigned char) type
- The buffer houses the packet
- The COAP_HEADER_VERSION_MASK value is 0xC0
- COAP_HEADER_VERSION_POSITION is 6
- the type value is an enum structure with 4 values
- COAP_HEADER_TYPE_MASK value is 0x30
- COAP_HEADER_TYPE_POSITION value is 4
Now according to CoAP RFC 7252 the both the version and the type occupy two bits and so the mask operations and used values make sense to me.
My question is why are the shifting values different in the two operations ?,does it have something to do with one using an unsigned char and another the enum structure?